I am trying to read in a text file using fscanf. The input file looks like this:
1
Test
0
32.1
Everything is read fine except the first integer which gets read as some random value.
My code:
while((fgets(string,64,fp) != NULL)){
fscanf(fp, "%i\n", &tid);
fscanf(fp, "%s\n", name);
fscanf(fp, "%d\n", &stat);
fscanf(fp, "%f\n", &per);
printf("%d %s %d %f", tid, name, stat, per);
}
Output:
11281472 Test 0 32.0999982
Does anyone know what I do wrong?
Entire function for reference:
Task *readData(Task *strt, char *fname){
#ifdef DEBUG
fprintf(stderr, "Entered Data import method\n");
#endif
char name[30];
int tid, stat;
float per;
FILE *fp;
fp= fopen(fname, "r");
if(fgetc(fp) == EOF){
printf("File is empty");
}
else{
while(!feof(fp)){
fscanf(fp, "%i\n", &tid);
fscanf(fp, "%s\n", name);
fscanf(fp, "%d\n", &stat);
fscanf(fp, "%f\n", &per);
printf("%d %s %d %f", tid, name, stat, per);
strt = AddB(strt, tid, name, stat, per);
}
}
return (strt);
}
fgetsdoes a reading (consuming) of first string in the input file and then firstfscanftries to parse “Test” as “%i” format and fails.You may use
feofas condition in while loop, but (thnx to John Bode) it is better to check every fscanf and use a flag orbreak.Every
fscanfreturns a value. As stated in 1:So, you may want to check the return value of every fscanf to know that something goes (reads) wrong.
UPDATE:
You should never use fgets/fgetc to check something (without rewinding back with
fseekor undoing withungetc), because every file read function will CONSUME data from file (current position in file will be incremented). To check errors, just look at return value of usual fscanf/other function you used to parse input.