Im working on a program that opens a file of student records and stores them in an array of structures.
My structure looks like this
Right now im having trouble going through the data in the file and storing it.Code:typedef struct { char name[21]; char studentID[5]; int scoreOne; int scoreTwo; int scoreThree; int scoreFour; float average; char grade; } StuRec;
The data looks like thisI am using a loop with fgets and sscanf, which will stop at each new line because of fgets and store the string in a buffer then im trying to use sscanf to go through the buffer data and pull out what I need.Code:Cool, Joe:535987:100:43:68:88:C Slacker, Rufus:734120:65:48:0:68:F Smart, Maxwell:868686:55:64:99:99:C+
I can get through the name and student id(the first two pieces of data separated by : ) but am stuck on how to store the next four scores.
My code for the first part isIt should read up to 20 characters or stop at a colon. Then skip the colon and read 6 more or stop at the next colon. This is where im starting to get lost. I need to find the average of the next four numbers to be part of my structure as well. What is the best way to read in these numbers so I can also find their average? Additionally the scores can have a range from 0 to 150Code:sscanf(buffer, "%20[^:]%[:]%6[^:]%[:]", stuAry[i].name, stuAry[i].studentID);
I think I can get through the letter grade at the end. I was thinking of something likeor even just simplyCode:%2[ABCDF+-]thanksCode:%2s



LinkBack URL
About LinkBacks



