Sign in to follow this  

file I/O and structs in C problem

This topic is 4104 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

alright, i have a simple struct--- typedef struct studentRecord { char firstName[20]; char lastName[20]; int ID; Course* courseInfo; // pointer to allocated course information } Student; and where Course is another struct (typedef struct courseInfo) with other arbitrary members. I have a text file that i want to read with fscanf, and the structure of that file is just "name name number number" ex( josh howard 5432 30); I allocated the memory for an array of Students. And all i seem to not be able to do is read from the file a quiz grade and store it to Quiz[10] in the Course struct. For instance, I can read in 2 strings and a number from student (first name, last name, ID) but when i try to put the last number into a quiz[] array in Course, the program freezes. for (i = 0; i < numberOfRecords; i++) { while( fscanf("%s %s %d", &studentArray[i].lastName, &studentArray[i].firstName, &studentArray[i].ID)== 3){ do whatever } } // works fine but... for (i = 0; i < numberOfRecords; i++) { while( fscanf("%s %s %d %d", &studentArray[i].lastName, &studentArray[i].firstName, &studentArray[i].ID, &studentArray[i].courseInfo->Quiz[0])== 4){ do whatever } } // freezes the file that it is reading from is not the problem, but i cant figure out what is and im sure its an easy one.

Share this post


Link to post
Share on other sites
Before you call fscanf, did you allocate some memory for studentArray[i]->courseInfo? fscanf will not allocate memory for you.

something like:


studentArray[i].courseInfo = (Course*)malloc(sizeof(Course)*MAX_COURSES);
while (fscanf(.......) ) { }


or


studentArray[i].couuseInfo = new Course[MAX_COURSES];
while (fscanf(...... ) ) { }

Share this post


Link to post
Share on other sites

This topic is 4104 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this