How to auto create filename and index them?

17 Feb 2010

Dear all,

I am building a data logger now. I can save all data (analog inputs) to a filename called "temp1.csv" in local file system upon triggering a button. (based on "how cold?")

Say, I need to keep the file and acquire new set of data. How to keep the files in RAM and do not overwrite the same file? I was thinking how to read filename in local file system with extention .csv, then +1 to the maxium file number.

For example, temp1.csv --> temp2.csv  --> temp3.csv ....

Cheers.

17 Feb 2010

You can use, for example, the sprintf function:

int file_no;
char filename[256];

write_file()
{
  sprintf(filename, "/local/temp%d.csv", file_no);
  FILE *fout = fopen(filename, "w");
  if ( fout != NULL )
  {
    //write stuff
    fclose(fout);
    file_no++;
  }
}

void main()
{
  // start from 1
  file_no = 1;
  button.attach(write_file);
  while(1);
}

 

22 Feb 2010

thank you.

I  have used "sprintf" to assign character to a data type char. Instead of using your recommendation, I use yours in http://mbed.org/forum/mbed/topic/463/?page=1#comment-2354. I did:

check every filename (I assigned in a loop)

return: found or not found,

if not, that is the next file  I would use for data logger.

This is my code:

***************************************

#include "mbed.h"
#include "TextLCD.h"

LocalFileSystem local("local");
TextLCD lcd(p24, p25, p26, p27, p28, p29, p30); // rs, rw, e, d0, d1, d2, d3
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalIn button(p12);


// for the datalogger, the filename is: name + no.
// say, temp1.csv, temp2.csv, ...
// based on program: find_fie1
// loop a incremental counter until filename is not found.
// the last number will be the file name.

// return true if file (or directory) 'filename' is present in the directory 'root'

 

char direct[]= "local";
char file[20];
int file_no;
int count;
bool not_found;

bool exists(char * root, char *filename) {

    DIR *d;
    struct dirent *p;
    d = opendir("/local");

    //printf("\nList of files in the directory %s:\n", root);
    bool found = false;
    if ( d != NULL ) {
        while ( !found && (p = readdir(d)) != NULL ) {
            //printf(" - %s\n", p->d_name);
            if ( strcmp(p->d_name, filename) == 0 )
                found = true;
        }
    }
    closedir(d);
    not_found=!found;
    return found;
}

 


// this routine could check the last number of file it is
// last file _no = file_no - 1;

int main( void ) {
    //while (1) {
        not_found=false;
        led1=button;
        file_no=1;
//        while (button==false) {
          while (not_found==false) {


            sprintf(file, "TEMP%d.CSV", file_no);

            //wait(2); // just to see display at LCD
            if ( exists(direct, file) ) {
                printf ("Found myfile.txt\n");
                lcd.cls();
                lcd.locate(0,0);
                lcd.printf("Found %s",file);
            } else {
                printf ("Not found myfile.txt\n");
                lcd.cls();
                lcd.locate(0,0);
                lcd.printf("Not %s", file);
            }
            file_no++;
            lcd.locate(0,1);
            lcd.printf("%d", file_no-1);
       // }

    }

}

23 Mar 2010

Igor Skochinsky wrote:

You can use, for example, the sprintf function:

int file_no;
char filename[256];

write_file()
{
  sprintf(filename, "/local/temp%d.csv", file_no);
  FILE *fout = fopen(filename, "w");
  if ( fout != NULL )
  {
    //write stuff
    fclose(fout);
    file_no++;
  }
}

void main()
{
  // start from 1
  file_no = 1;
  button.attach(write_file);
  while(1);
}

 

For this code, I think there are two problems:

1.  Explicity type is missing for the function of write_file().  So should that be int write_file()?

2.  For 'button.attach(write_file)', identifier button is undefined. How can I defined the button.