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.