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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 11:47 AM.

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, 02:51 PM
  2. Problem with formatting output
    By williamsb52 in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2003, 07: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, 06:42 PM