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;
}