fscanf not working. Getting all 0 values in variables after scan which means scanf function did not execute

29 Nov 2018

I am using fscanf function to get values from the afe.csv file stored in SD card reader.

i am not able to read the values and getting 0 for all parameters when printed on screen.

please find below the code for reference.

void read_file()

{ FILE *tmp; tmp= fopen("/sd/afetest.csv", "r"); if(tmp==NULL) represent file is present { serial.printf("file not present\n"); } {

while(!feof(tmp)) { int z=0; z = vfscanf(tmp,"%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f \r\n",&Total_loop_time1,&cellvolt[0],&cellvolt[1],&cellvolt[2],&cellvolt[3],&cellvolt[4],&cellvolt[5],&cellvolt[6],&cellvolt[7],&cellvolt[8],&cellvolt[9],&cellvolt[10],&cellvolt[11],&cellvolt[12],&cellvolt[13],&cellvolt[14], &bat_volt,&battery_current,&Bat_Ah,&soc,&temp_deg3,&temp_deg2,&temp_deg1); serial.printf("\n\n z is %d \r\n ", z); {

} } } fclose(tmp); }

11 Dec 2018

Hi Imran,

It's not obvious what the problem is, so we suggest first reading the file line by line with fgets() or reading the individual entries as strings to make sure that you are at least properly reading from the file!

Interestingly we happened to come across a very similar question from 2011:

Good Luck,

Ralph, Team Mbed