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,"%[^]%[^;];%[^;];%[^;]",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!