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);
}
fgetsbefore the firstfscanf, what does that load intostring? - Mr Listerwhileloop of and everything works fine! - Pavan Manjunath