Jump to content
  • Advertisement
Sign in to follow this  
josh howard

file I/O and structs in C problem

This topic is 4475 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.lastName, &studentArray.firstName, &studentArray.ID)== 3){ do whatever } } // works fine but... for (i = 0; i < numberOfRecords; i++) { while( fscanf("%s %s %d %d", &studentArray.lastName, &studentArray.firstName, &studentArray.ID, &studentArray.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
Advertisement
Before you call fscanf, did you allocate some memory for studentArray->courseInfo? fscanf will not allocate memory for you.

something like:


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


or


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!