Some questions on sscanf and parsing data

This is a discussion on Some questions on sscanf and parsing data within the C Programming forums, part of the General Programming Boards category; Im working on a program that opens a file of student records and stores them in an array of structures. ...

  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.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And %d doesn't work because?

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    13
    can I use the same string conversions with %d? Im really new with this...

    something like?
    Code:
    %3[^:]d

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    No. Fortunately you don't want to, since %d will stop at a colon anyway, without your help.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    13
    wooo thanks a lot ill give this another shot now

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Your struct has some problems, too. studentID should be longer and so should grade. Try this sscanf line:

    Code:
    sscanf(buffer, "%[A-za-z ,]:%[0-9]:%d:%d:%d:%d:%s",StuRec.name,StuRec.studentID,
                &StuRec.scoreOne,&StuRec.scoreTwo,&StuRec.scoreThree,
                &StuRec.scoreFour,StuRec.grade);
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    13
    thanks guys

    MK27 that was it. I was making things WAY harder than I needed to. Thanks a lot

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by green2black View Post
    thanks guys

    MK27 that was it. I was making things WAY harder than I needed to. Thanks a lot
    Some of the sscanf documentation I've seen is very heavy on possibilities, yet extremely lite on examples, so this isn't surprising.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

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, 01: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, 07:36 AM
  5. sscanf string parsing
    By penney in forum C Programming
    Replies: 5
    Last Post: 02-03-2003, 11:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21