fscanf and feof issue

13 Nov 2010

I wanna save a config file in the local file system. The fprintf part is working OK but scanf is not working. There is also an issue with the feof function because the program hangs in the while loop.

This is my code:

#include "mbed.h"

LocalFileSystem local("local");   //File System
int main() {

FILE *tmp = fopen("/local/tmp.txt", "w");  // Open "temp.txt" on the local file system for writing

char line=0;
double max[3],min[3],offset[3], range[3];

max[0]=250;
max[1]=230;
max[2]=210;

min[0]=-234;
min[1]=-154;
min[2]=-238;

range[0]=max[0]-min[0];
range[1]=max[1]-min[1];
range[2]=max[2]-min[2];

offset[0]=range[0]/2-max[0];
offset[1]=range[1]/2-max[1];
offset[2]=range[0]/2-max[2];

if ( tmp == NULL)
error("Could not create file");


fprintf(tmp, "%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n", max[0], min[0], offset[0], range[0],max[1], min[1], offset[1], range[1],max[2], min[2], offset[2], range[2] );

fclose(tmp); 

max[0]=0;
min[0]=0;
offset[0]=0;
range[0]=0;
max[1]=0;
min[1]=0;
offset[1]=0;
range[1]=0;
max[2]=0;
min[2]=0;
offset[2]=0;
range[2]=0;

//update config file
FILE * fp = fopen("/local/config.txt", "w");  // Open "config.txt" on the local file system for writing
tmp = fopen("/local/tmp.txt", "r");  // Open "temp.txt" on the local file system for read

if ( tmp == NULL)
error("Could not open file");

while (!feof(tmp))
{
fscanf(tmp, "%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n",  &max[0], &min[0], &offset[0], &range[0],&max[1], &min[1], &offset[1], &range[1],&max[2], &min[2], &offset[2], &range[2]) ;
fprintf(fp, "%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n",  max[0], min[0], offset[0], range[0],max[1], min[1], offset[1], range[1],max[2], min[2], offset[2], range[2]) ;

}

fclose(fp); 
fclose(tmp); 

}

 

Files output:

TMP.txt:

250.00    -234.00    -8.00    484.00    230.00    -154.00    -38.00    384.00

 

CONFIG.txt

0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
and so on

 

I need help as soon as posible. It is part of my senior design project. Thanks!

 



13 Nov 2010 . Edited: 13 Nov 2010

You need %lf (lowercase L) to read a double.

13 Nov 2010

Hi Jose,

First thing to check is whether fscanf is reading properly. You can check the return value of fscanf, as this returns the number of values it successfully read.

If you do check this, you'll find it returns "0" i.e. it is not reading any items successfully. And as it never reads anything, it'll never get to the end of the file, hence the infinite loop!

Your code is almost there, you've just got a couple of bugs in your fscanf/fprintf code:

  • You use a format specifier on reading (%.2f), which should not be used (just use %f)
  • You are reading floats, but defined them as doubles (so I just changed the type of the variables to float
  • Also, you are trying to read/write 12 variables, but only have 8 in the format string

With these changes, the resulting code is:

#include "mbed.h"

LocalFileSystem local("local");   //File System
int main() {

    FILE *tmp = fopen("/local/tmp.txt", "w");  // Open "temp.txt" on the local file system for writing

    float max[3],min[3],offset[3], range[3];

    max[0]=250;
    max[1]=230;
    max[2]=210;

    min[0]=-234;
    min[1]=-154;
    min[2]=-238;

    range[0]=max[0]-min[0];
    range[1]=max[1]-min[1];
    range[2]=max[2]-min[2];

    offset[0]=range[0]/2-max[0];
    offset[1]=range[1]/2-max[1];
    offset[2]=range[0]/2-max[2];

    if ( tmp == NULL)
        error("Could not create file");


    fprintf(tmp, "%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n",
            max[0], min[0], offset[0], range[0],max[1], min[1], offset[1], range[1],max[2], min[2], offset[2], range[2] );

    fclose(tmp);

    max[0]=0;
    min[0]=0;
    offset[0]=0;
    range[0]=0;
    max[1]=0;
    min[1]=0;
    offset[1]=0;
    range[1]=0;
    max[2]=0;
    min[2]=0;
    offset[2]=0;
    range[2]=0;

    // update config file
    FILE * fp = fopen("/local/config.txt", "w");  // Open "config.txt" on the local file system for writing
    tmp = fopen("/local/tmp.txt", "r");  // Open "temp.txt" on the local file system for read

    if (tmp == NULL) {
        error("Could not open file");
    }
    
    while (!feof(tmp)) {
        int n = fscanf(tmp, "%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\n",
            &max[0], &min[0], &offset[0], &range[0],&max[1], &min[1], &offset[1], &range[1],&max[2], &min[2], &offset[2], &range[2]);

        if (n != 12) {
            error("Could not read 12 elements");
        }

        fprintf(fp, "%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n",
                max[0], min[0], offset[0], range[0],max[1], min[1], offset[1], range[1],max[2], min[2], offset[2], range[2]);
    }

    fclose(fp);
    fclose(tmp);
}
which appears to work exactly as you intended!

Hope that helps,

Simon

14 Nov 2010

Thank you very much Simon. It helped me a lot!

14 Nov 2010

 

Ivo,

The %lf is working OK, too. Thanks!!!

 

20 Feb 2011

Hi Simon,

Thank you very much! It helped me tooooo!

Regards, Rainer