My Output, cant get it right using sscanf in parsing, whats my problem??

This is a discussion on My Output, cant get it right using sscanf in parsing, whats my problem?? within the C Programming forums, part of the General Programming Boards category; I read a couple of threads lying around the net on parsing strings and such. I came about sscanf as ...

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

    Question My Output, cant get it right using sscanf in parsing, whats my problem??

    I read a couple of threads lying around the net on parsing strings and such. I came about sscanf as a best method, im gettin close to what i want but my output is not completely correct. My input text is as such:

    citystats.txt

    San Ignacio;
    Cayo;
    4000;
    5;

    Thats my text file im reading infrom from, and on my output i get this with some other zeros which i cant seem to take off, whats my problem. Im kinda new to structures. Between if u guys have a better of suggestions on doing this please feel free to advice. Thanks in advance loves! I know it has something to do with my looping, i believe my i's not sure though

    Sorry forgot to put the output, this is it:

    San Ignacio
    00Cayo
    004000
    00500000000000000

    Thats what i get!

    Here is my code:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main (void)
    {
    
      struct stats {
        char town [32];
       char district[32];
        int population;
        int quality;
       };
    
      struct stats stat [100];
      char line [50];
      int i=0;
    
     char filename[]="citystats.txt";
     FILE *file=fopen(filename,"r");
      if (file)
      {
         while (fgets(line,sizeof line,file))
        
         sscanf(line,"&#37;[^]%[^;];%[^;];%[^;]",stat[i].town,stat[i].district,&stat[i].population,&stat[i].quality);
       
        i++;
    }
    
      fclose (file);
    
     for (i=0;i<10;i++){
    
     printf("%s%s%d%d",stat[i].town,stat[i].district,stat[i].population,stat[i].quality);
    }
    
    return 0;
    
    }
    Im a bit frustrated, i dnt have my orignal codes with me, i had copied it on paper to post, but i thnk i might have copied parts wrong!
    Last edited by AvaGodess; 10-16-2008 at 12:47 PM.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You would help us by also posting your output so we know what to look for without having to parse your whole program ourselves.

    Dino - official representative of the lazy masses.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    Is that your code?
    Because it doesn't even compile.

    Oh, and add some more braces, so your i++ is part of the while loop.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    54
    Quote Originally Posted by Dino View Post
    You would help us by also posting your output so we know what to look for without having to parse your whole program ourselves.

    Dino - official representative of the lazy masses.

    Sorry i forgot bout the output! The output is this:

    San Ignacio
    00Cayo
    004000
    00500000000000000

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    54
    Quote Originally Posted by Salem View Post
    Is that your code?
    Because it doesn't even compile.

    Oh, and add some more braces, so your i++ is part of the while loop.
    What do u mean it doesnt compile, i get output and all. i just posted the output i get!

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    54
    Quote Originally Posted by Salem View Post
    Is that your code?
    Because it doesn't even compile.

    Oh, and add some more braces, so your i++ is part of the while loop.
    Sorry matey, i had done a mistype on the codes i posted i already changed it. U were right it wudnt compile!

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    54
    Quote Originally Posted by Dino View Post
    You would help us by also posting your output so we know what to look for without having to parse your whole program ourselves.

    Dino - official representative of the lazy masses.
    Hey i had mistyped parts of my code, i re did it, its there as i have it now

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    This won't compile...
    Code:
     printf("&#37;s%s%d%d,stat[i].town,stat[i].district,stat[i].population,stat[i].quality);
    Try again.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    54
    Quote Originally Posted by Dino View Post
    This won't compile...
    Code:
     printf("&#37;s%s%d%d,stat[i].town,stat[i].district,stat[i].population,stat[i].quality);
    Try again.
    Sorry forgot the double quotes after the %d, i remodified it AGain! Sorry, its cuz i dnt have my orignal codes here, could u have another look pls love, Hey again matey, really sorry i had an o instead of a zero, i mistyped, i just typed my codes like i said, i have no access to a compiler right now!
    Last edited by AvaGodess; 10-16-2008 at 12:50 PM.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    You shouldn't be doing any retyping, it should be a cut-paste job.
    If you continue to post approximate questions, expect approximate answers.

    Either wait until you have the original code to hand, or find a way to get the code.

    > sscanf(line,"&#37;[^]%[^;];%[^;];%[^;]",stat[i].town,stat[i].district,&stat[i].population,&stat[i].quality);
    If your input file is across 4 lines, as it seems to be, then you need to read 4 lines, and extract each line into each field.

    The last two conversions in this sscanf should have been %d anyway.

    Like I said, we're just pulling random faults, because it's impossible to tell which part is the real problem
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Posts
    54
    Quote Originally Posted by Salem View Post
    You shouldn't be doing any retyping, it should be a cut-paste job.
    If you continue to post approximate questions, expect approximate answers.

    Either wait until you have the original code to hand, or find a way to get the code.

    > sscanf(line,"%[^]%[^;];%[^;];%[^;]",stat[i].town,stat[i].district,&stat[i].population,&stat[i].quality);
    If your input file is across 4 lines, as it seems to be, then you need to read 4 lines, and extract each line into each field.

    The last two conversions in this sscanf should have been %d anyway.

    Like I said, we're just pulling random faults, because it's impossible to tell which part is the real problem
    Hey Salem i understand u, i thnk ill get to my pc better in a while and start a new thread, and get rid of this confusion, just my code and paste it like u said. One short question though, on the sscanf. do u mean the last %[^;] should actually be something like %d[^;]

    just asking cuz im not really sure how to use sscanf, i came about on it on a recent thread! Could u just pls let me know!

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by AvaGodess View Post
    Hey Salem i understand u, i thnk ill get to my pc better in a while and start a new thread, and get rid of this confusion, just my code and paste it like u said. One short question though, on the sscanf. do u mean the last %[^;] should actually be something like %d[^;]

    just asking cuz im not really sure how to use sscanf, i came about on it on a recent thread! Could u just pls let me know!
    If you know how to use scanf, you know how to use sscanf.

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by AvaGodess View Post
    Thats what i get!
    What do you want? If it's taking out the zero's, your output will be almost exactly the same as your input (minus some semi-colons).
    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. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  2. Problem with formatting output
    By williamsb52 in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2003, 08:34 PM
  3. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 06:02 PM
  4. output from bank type problem
    By IzaakF in forum C Programming
    Replies: 2
    Last Post: 09-04-2002, 07:42 PM
  5. File input output problem
    By VanJay011379 in forum C++ Programming
    Replies: 2
    Last Post: 08-30-2002, 03:09 PM

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