Thread: help with sscanf

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    48

    help with sscanf

    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    #define MAX 21
    #define MAX_ELEM 8
    #define SCORE 12
    #define NUM_SKATER 4
    #define BASE 3.1
     
    typedef struct{
                    char  name[MAX];
                    int   elements;
                    float baseval[MAX_ELEM];
                    int score[MAX_ELEM][SCORE];
                    float total_base;
                    float tech_score;
                    float total_score;
                  }SKATER;
     
    int  getData(SKATER skater[NUM_SKATER]);
    void calcData(SKATER skater[NUM_SKATER]);
    void insertSort(SKATER skater[NUM_SKATER], int num);
    void printData(SKATER skater[]);
     
    int main (void)
    {
            // Global Declarations
            SKATER skater[NUM_SKATER];
            int num;
     
            // Funtion calls
            num = getData(skater);
            calcData(skater);
            insertSort(skater, NUM_SKATER);
            printData(skater);
            return 0;
    }
     
    /********************************* getData ************************************
    Pre:
    Post:
    */
    int getData(SKATER skater[NUM_SKATER])
    {
            // LOcal Declarations
            FILE* fpIn;
            int   i = 0;  
            int       k;
            int   j;
            char  buffer[256]; 
     
            // Statements
            if((fpIn = fopen("lab6data.txt","r"))==NULL)
            {
                    printf("File opening error");
                    system("PAUSE");
                    exit(100);
            }
         
            while(i < NUM_SKATER && fgets(buffer, sizeof(buffer) - 1, fpIn))
            {               
                    sscanf(buffer,"%*c%19[^0123456789]", skater[i].name);                   
                    for(k = 0; k < MAX_ELEM; k++)
                    {                       
                            if(fgets(buffer, sizeof(buffer)-1, fpIn) != NULL)
                        {                    
                                    sscanf(buffer,"%d %f", &skater[i].elements, &skater[i].baseval[k]);
                                    for(j = 0; j < SCORE; j++)
                                    {
                                            sscanf(buffer,"%d", &skater[i].score[k][j]);
                                    }
                            }
                    } 
                
                    i++;            
            }
     
            system("PAUSE");
            fclose(fpIn);
     
            return i;
    }
    so i have this problem reading data using sscanf, it seems that sscanf read the same buffer the same way, so that it always start at the beginning of the buffer. When i tried to read data for the 2d score array it read in the elements number which is the beginning of the buffer as i read them in second sscanf how do i fix this code so that i would read the score instead of the element numbers?

    the input data is in the link
    Ideone.com | Online C Compiler & Debugging Tool
    one each line the first number being the element number the second is the base val and the rest is the scores
    thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > sscanf(buffer,"%d", &skater[i].score[k][j]);
    If you're trying to "walk" through a set of numbers all on one line, then use strtol(), which updates a pointer to where it got to.

    You can achieve the same effect by using the %n conversion in sscanf - look it up.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    You should use the %n format in sscanf like

    Code:
    while(i < NUM_SKATER && fgets(buffer, sizeof(buffer), fpIn))
    {              
      sscanf(buffer,"%20[^\n]", skater[i].name);                  
      for(k = 0; k < MAX_ELEM; k++)
      {                      
        if(fgets(buffer, sizeof(buffer), fpIn) != NULL)
        {
          char *p=buffer;
          int n=0;                   
          if(2!=sscanf(p+=n,"%d%f%n",&skater[i].elements,
                                                  &skater[i].baseval[k],&n)) exit(1);
          for(j = 0; j < SCORE; j++)
          {
            if( 1!=sscanf(p+=n,"%d%n", &skater[i].score[k][j],&n) ) exit(1);
          }
        }
      }
      i++;           
    }
    Your "-1" in fgets is redundant.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sscanf help
    By abulreesh in forum C Programming
    Replies: 3
    Last Post: 12-10-2006, 11:03 AM
  2. Using sscanf
    By scaven in forum C Programming
    Replies: 1
    Last Post: 04-13-2003, 08:34 PM
  3. sscanf help!!
    By PunkyBunny300 in forum C Programming
    Replies: 4
    Last Post: 02-27-2003, 02:45 AM
  4. sscanf
    By Roaring_Tiger in forum C Programming
    Replies: 2
    Last Post: 02-11-2003, 06:58 PM
  5. SSCANF help
    By mattz in forum C Programming
    Replies: 7
    Last Post: 12-10-2001, 04:53 PM