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.