How can I append data in the same file?

29 Mar 2011

Hi I am using ticker with 1 min interval. With in the ticker function I am opening one text file to store 6 analog data and time. like this 1: 1300971050,2.175,2.174,2.176,2.169,2.177,2.168

I need 60 datas like above in the same file. How can I append in the same file. At 1st min file is opening.After that the file is not opening.File becomes empty. So I cann't get all the 60 data.(60 datas should be in the same file).

Kindly help me...

int main()  
{
ADC1.attach(&read_ADC1, 1.0);
}
void read_ADC1()
        {
      
        FILE *fp = fopen("/local/analog.txt","w");  

        avg[0] = (float)ain_UseA.read_u16()* vdiv;
        avg[1] = (float)ain_UseB.read_u16()* vdiv;
        avg[2] = (float)ain_UseC.read_u16()* vdiv;
        avg[3] = (float)ain_UseD.read_u16()* vdiv;
        avg[4] = (float)ain_UseE.read_u16()* vdiv;
        avg[5] = (float)ain_UseF.read_u16()* vdiv;
        
        time_t seconds = time(NULL); 
 
        
        fprintf (fp, "%d %5.3f %5.3f %5.3f %5.3f %5.3f %5.3f",seconds, avg[0],avg[1],avg[2],avg[3],avg[4],avg[5]);
         fclose(fp);
            }
29 Mar 2011

Your program terminates so I have no idea what the ticker callback behaviour for a terminated program is (Simon?)

However, you are best doing the file write in user context.

This may or may not work, just a snippet:-

#include "mbed.h"

// Need to define local file system, you didn't include in your snuippet above
// so add that here.

Ticker ADC1;

volatile bool ticked;
void ticker_callback(void) { ticked = true; }

int main() {
    ticked = false;
    ADC1.attach(&ticker_callback, 60); // Note, 60seconds is 1 minute.

    // Never let your program end, loop forever!
    while(1) {
        if (ticked) {
            ticked = false;
            FILE *fp = fopen("/local/analog.txt","w+");  
            avg[0] = (float)ain_UseA.read_u16()* vdiv;
            avg[1] = (float)ain_UseB.read_u16()* vdiv;
            avg[2] = (float)ain_UseC.read_u16()* vdiv;
            avg[3] = (float)ain_UseD.read_u16()* vdiv;
            avg[4] = (float)ain_UseE.read_u16()* vdiv;
            avg[5] = (float)ain_UseF.read_u16()* vdiv;
            time_t seconds = time(NULL);     
            fprintf (fp, "%d %5.3f %5.3f %5.3f %5.3f %5.3f %5.3f",
                seconds, avg[0],avg[1],avg[2],avg[3],avg[4],avg[5]);
            fclose(fp);
        }
    }
}    

[edit, added volatile to the bool ticked, added "w+" to append to file. "w" will overwrite an existing file.]

29 Mar 2011

I'm using this to append - note that it creates the file if it does not already exists:

FILE *log = fopen("/local/speedlog.txt", "a"); Open "speedlog.txt" on the local file system for appending ('a' switch)

fprintf(log," %.1f \n", frequency); write value and newline

fclose(log); close file

29 Mar 2011

@David, ah "a" :)

The point I was trying to assert in my snippet was to move the actual file write out of the callback function and into main(). Doing things like that in a callback can cause problems.