sscanf problem, program modified but with slight problem still, please help!

This is a discussion on sscanf problem, program modified but with slight problem still, please help! within the C Programming forums, part of the General Programming Boards category; As i had stated on a previous thread, i found out that sscanf is better when parsing string literals, my ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    54

    Question sscanf problem, program modified but with slight problem still, please help!

    As i had stated on a previous thread, i found out that sscanf is better when parsing string literals, my program compiles but the output is not what i expect.

    My input file is as follows: CityStats.txt with the following data

    San Ignacio;Cayo;4000;5

    and my output comes out as :

    San Ignacio
    Cayo
    808464436
    50921525

    i got no idea how i get those final results. Can anyone help, i tried making it as simple as possible. the last two output should be 4000 and finally 5, but there is some wierd problems. advice, suggestions anyone, here is the code i have:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
        
        struct stats {
           char town [32];
           char district [32];
           int pop;
           int quality;
           };
           
        
         struct stats stat;
         char line[50]; /* space to read a line into */
         int result;
         
         
         char filename[] = "CityStats.txt"; /* the name of a file to open */
         FILE *file = fopen(filename, "r"); /* try to open the file */
         if ( file )
        {
          
          while ( fgets(line, sizeof line, file) ) /* read each line */
          {
             result=sscanf(line, "%[^;];%[^;];%[^;];%[^;]",stat.town,stat.district,&stat.pop,&stat.quality);
          }
         
          fclose(file);
       }
       printf("sscanf successfully converted %d items!\n\n",result);
       
    
       printf("%s\n%s\n%d\n%d", stat.town, stat.district, stat.pop,stat.quality);
       
    
         
     return 0;
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Try this sscanf line instead:

    Code:
    result=sscanf(line, "&#37;32[ a-zA-Z];%32[ a-zA-Z];%d;%d",stat.town,stat.district,&stat.pop,&stat.quality);
    If there is a newline in CityStats.txt, result will be 0 because sscanf read the next (empty) line.

    ps. anything involving square brackets is about characters and you can't put that into an int
    Last edited by MK27; 10-16-2008 at 05:24 PM.
    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

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    54
    Hey i got that to work, thank you very much. I got a question, imagine id have another line on the text file such as:

    Halie Villie;Hampshire;2000;3
    Hell,Heaven,800,4

    how would i go about doin the same thing as what i did on the top. Now would if statements be needed to look up new line characters. Any ideas!

    Thanks by the way it did help

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I would use strtok for this. That way, you can set the delimiter to ";," which will cover both cases.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
        
        struct stats {
           char town [32];
           char district [32];
           int pop;
           int quality;
           };
           
        
         struct stats stat;
         char line[50]; /* space to read a line into */
         
         char filename[] = "CityStats.txt", *tok;
         FILE *file = fopen(filename, "r"); /* try to open the file */
         if ( file )
        {
          
          while ( fgets(line, 50, file) ) /* read each line */
          {
    	tok=strtok(line,";,");
    	strcpy(stat.town,tok);
    	tok=strtok(NULL,";,");
    	strcpy(stat.district,tok);
    	tok=strtok(NULL,";,");
    	stat.pop=atoi(tok);
    	tok=strtok(NULL,";,");
    	stat.quality=atoi(tok);
    	printf("%s\n%s\n%d\n%d\n", stat.town, stat.district, stat.pop,stat.quality);
          }
         
          fclose(file);
       }
     return 0;
    }
    There is no "result" measurement in this one.
    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

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    54
    Quote Originally Posted by MK27 View Post
    I would use strtok for this. That way, you can set the delimiter to ";," which will cover both cases.

    There is no "result" measurement in this one.

    Mk Thank you very much, that has been very helpful.Previously before i started this task i was gona use that, but i read on a thread that sscanf was better and took this, but hey that works beautifully. MK, i would like a favor im trying to use this as a small quiz for my friends and they are all here on the forums, thnk u cud edit ur message and remove the codes. I'd appreciate it, thank u in advance and for all the help u have given me!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-25-2008, 12:31 AM
  2. Program problem
    By Birdhaus in forum C++ Programming
    Replies: 6
    Last Post: 11-21-2005, 09:37 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. simple frontend program problem
    By gandalf_bar in forum Linux Programming
    Replies: 16
    Last Post: 04-22-2004, 06:33 AM
  5. Console Program Problem
    By Breach23 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 12:35 AM

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