Simon Ford / Mbed 2 deprecated IncrementingFilename

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // a way to create incrementing filenames, sford
00002 // every time you hit reset, will create a file with incremental filename
00003 
00004 #include "mbed.h"
00005 
00006 LocalFileSystem local("local");               // Create the local filesystem under the name "local"
00007 
00008 int main() {
00009     char filename[64];    
00010     int n = 0;
00011     
00012     // set "filename" equal to the next file to write
00013     while(1) {
00014         sprintf(filename, "/local/file%03d.txt", n);    // construct the filename fileNNN.txt
00015         FILE *fp = fopen(filename, "r");                // try and open it
00016         if(fp == NULL) {                                // if not found, we're done!
00017             break;
00018         }
00019         fclose(fp);                                     // close the file
00020         n++;                                            // and try the next one
00021     }
00022     
00023     FILE *fp = fopen(filename, "w");
00024     fprintf(fp, "I am file # %d\n", n);
00025     fclose(fp);
00026 }