write files to disk

22 Feb 2011

Hi,

Is it possible to write a file to the mbed without overwriting the old one.I'm writing the values from an array to a local file. But when i do another measurment, i want to write another file (thus preventing the exising one to be overwritten).

void WriteVal() {
    // Writing the values in array to file onto disk.
    pc.printf("Writing to disk (be patient) \n\r");
    FILE *fp = fopen("/local/testfile.txt", "w");
    if (!fp) {
        fprintf(stderr, "File could not be openend \n\r");
        exit(1);
        main();
    }

    wait(2.0);

    for (int i=0; i < NR_SAMPLES; i++) {

        fprintf(fp," %i \n\r", samples[i]);
    }
    pc.printf("closing file");
    fclose(fp);
    wait(2.0);;
    main();
}

How can i solve this ?

22 Feb 2011

Well, just use another filename :)

22 Feb 2011

Obvious,

But then i have change the code everytime i write to file :). And i use it to write sampled data to a file. So it would be easier if in the code the filename is changed, say with date, time, everytime ik take a sample.

22 Feb 2011

You could probably do with something like this.

22 Feb 2011

MMM,

I have to eset the MBED everytime. Tommorow i will look further into it.

gr marcel

23 Feb 2011

How often do you plan to write to this file? Are you going to run out of file space on your drive or does this logging only rarely happen?

Quote:

I have to eset the MBED everytime.

You can also use the code that Igor linked to above in your WriteVal() routine. It doesn't have to be placed in main() to run on each reset.

23 Feb 2011

You could open the file with "a" or "a+" to append to the end of the file, instead of overwriting. If you need a time stamp, write one to the file before you log your data.

23 Feb 2011

I will try both options. Thx for all the input!