Thread: Some questions on sscanf and parsing data

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    13

    Some questions on sscanf and parsing data

    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
    Code:
    typedef struct
    	  {
    	    char name[21];
    	    char studentID[5];
    	    int scoreOne;
    	    int scoreTwo;
    	    int scoreThree;
    	    int scoreFour;
    	    float average;
    	    char grade;
    	  } StuRec;
    Right now im having trouble going through the data in the file and storing it.

    The data looks like this
    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 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.

    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 is
    Code:
    sscanf(buffer, "%20[^:]%[:]%6[^:]%[:]", stuAry[i].name, stuAry[i].studentID);
    It 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 150

    I think I can get through the letter grade at the end. I was thinking of something like
    Code:
    %2[ABCDF+-]
    or even just simply
    Code:
    %2s
    thanks
    Last edited by green2black; 12-02-2008 at 07:46 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parsing using strtok() and sscanf()
    By NuNn in forum C Programming
    Replies: 13
    Last Post: 02-12-2009, 02:43 PM
  2. Replies: 12
    Last Post: 10-16-2008, 12:07 PM
  3. sscanf and parsing strings
    By jco1323 in forum C Programming
    Replies: 4
    Last Post: 02-20-2008, 06:32 PM
  4. sscanf and parsing invalid floats?
    By Axel in forum C Programming
    Replies: 7
    Last Post: 10-20-2006, 06:36 AM
  5. sscanf string parsing
    By penney in forum C Programming
    Replies: 5
    Last Post: 02-03-2003, 11:28 AM