Like Tree2Likes
  • 2 Post By anduril462

Reading strings from file

This is a discussion on Reading strings from file within the C Programming forums, part of the General Programming Boards category; I'm currently working on a program for my Intro to C class, but I'm completely stumped on a bug I've ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    2

    Reading strings from file

    I'm currently working on a program for my Intro to C class, but I'm completely stumped on a bug I've been getting. The part I'm having trouble with is a function that reads in wedding guest information from a text file. The text file looks like this:
    Code:
    10 30
    BEN JOHNSON 4 2
    DOUG ESPINOSA 3 2
    SARAH TELLINGER 5 3
    GRANT THOMPSON 5 2
    JENNIFER WEST 7 6
    JACKSON JOHNSON 1 5
    MARTY MCFLY 4 1
    ELIZABETH JAMES 2 6
    MICKEY MOUSE 2 4
    RAJ SHAH 2 5
    The first line contains 2 integers - the number of families contained in the text file, and the capacity of the reception room (which is only used later in the program for the purpose of actually inviting the people). Then each line contains a first name, last name, number of people in the family, and priority number (which is also used to invite the families to the wedding later in the program).

    However, my function that reads the file and populates the 4 arrays seems to be getting hung up. Here's the function:
    Code:
    int readFile(char firstNames[MAX_NUM_FAMILIES][MAX_NAME_LENGTH],
                 char lastNames[MAX_NUM_FAMILIES][MAX_NAME_LENGTH],
                 int familySize[MAX_NUM_FAMILIES],
                 int priority[MAX_NUM_FAMILIES])
    {
        FILE* inputFile;
        inputFile = fopen("allguests.txt", "r");
    
    
        int i, currNumFamilies, roomCapacity;
        fscanf(inputFile, "%d", &currNumFamilies); // read in the number of families that we'll dump into the arrays
        fscanf(inputFile, "%d", &roomCapacity); // also obtain the capacity for the current room
    
    
        for(i = 0; i < currNumFamilies; i++)
        {
            fscanf(inputFile, "%s", firstNames[i]); // these 2 lines are
            fscanf(inputFile, "%s", lastNames[i]); // causing the crash
            fscanf(inputFile, "%d", familySize[i]);
            fscanf(inputFile, "%d", priority[i]);
        }
    
    
        for(i = 0; i < currNumFamilies; i++)
        {
            printf("%s %s %d %d\n", firstNames[i], lastNames[i], familySize[i], priority[i]);
        }
    
    
        fclose(inputFile);
    
    
        return roomCapacity;
    }
    I don't know if there's some subtlety of using fscanf with character arrays that I'm missing, but it's not even throwing me a warning. The code compiles, but the program just hangs up. Any help would be appreciated, as I've been mulling this over for a couple days without any success.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,462
    Quote Originally Posted by eganWall View Post
    I don't know if there's some subtlety of using fscanf with character arrays that I'm missing, but it's not even throwing me a warning. The code compiles, but the program just hangs up. Any help would be appreciated, as I've been mulling this over for a couple days without any success.
    Turn up the warnings on your compiler. It doesn't compile fine:
    Code:
    $ gcc -Wall -g -std=c99  invite.c   -o invite
    invite.c: In function ‘readFile’:
    invite.c:22: warning: format ‘%d’ expects type ‘int *’, but argument 3 has type ‘int’
    invite.c:23: warning: format ‘%d’ expects type ‘int *’, but argument 3 has type ‘int’
    The lines where you read familySize[i] and priority[i] should have a & in front of them. scanf needs the address of that specific array element to work properly.
    Salem and eganWall like this.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    2
    Well damn, that's a bit embarrassing. I sure learned my lesson from that one, and now it works perfectly. Thanks so much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading Strings from a text file?
    By Soxcrates in forum C++ Programming
    Replies: 7
    Last Post: 10-31-2010, 06:53 PM
  2. Help with reading strings from a file
    By ldcel in forum C Programming
    Replies: 3
    Last Post: 12-01-2007, 12:31 AM
  3. Reading Strings from a file.
    By RealLife in forum C++ Programming
    Replies: 16
    Last Post: 05-12-2007, 09:17 PM
  4. Reading in strings from a file..
    By Anti_Visual in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 02:56 AM
  5. Reading strings from a file
    By Capulet in forum C Programming
    Replies: 7
    Last Post: 12-04-2002, 03:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21