Why only 1 line is written in the text file?

17 Apr 2011

I am trying to save the analog input into text file. Any idea why it only has one line written in the text file.

Here is the codes: /media/uploads/bjsrlu/main.txt

A Cut and paste version:

#include "mbed.h" // read the analog input, time stamp it into seconds and then write to file.

AnalogIn input(p20);
Serial pc(USBTX, USBRX); // tx, rx

LocalFileSystem local("local");  // create the local file system under the name "local"
DigitalIn enable(p10);

int main() {
    FILE *fp = fopen ("/local/out.txt", "w"); // open out.txt" on the local file system for writing
    wait (5.0);
    unsigned short samples[1024];
    set_time (1303037151);
    int exit = 0;
        
    while (exit == 0) {
       time_t seconds = time (NULL);
       pc.printf ("Time as seconds since January 1, 1970 = %d , 0x%04X\n", seconds, input.read_u16() );
       fprintf (fp, "%d , 0x%04X\n", seconds, input.read_u16() );

     if (enable) {   // pull up VCC to stop the Analog Input Logging.
           exit = 1;
           wait (5); 
           pc.printf ("About to close file in 5 seconds....");
           fclose (fp);
           wait (5);
           pc.printf ("Halted...");
     }  
   
     wait_ms(200); 
     fclose (fp);
    }  // end of while (1)         
}   
17 Apr 2011

It looks like the fclose(fp) is inside your while loop so it would close the file at the end of the first iteration. Try moving fclose() outside of the while loop.

Hope that helps.

18 Apr 2011

Adam is right

In addition to what he said, remember the file needs to be opened in append mode.