filenaming

06 Aug 2010

Hi,

I have an issue with creating and naming files for data storage.

This is the method I use now:

char filename[64];
sprintf(filename, "/local/analog_%d.txt", x);
FILE *fp = fopen(filename, "w");
where x is an incremented variable which changes after a file has been opened, so that the next file is named the next highest number.

Unfortunately, this limits me to 10 files at most.

Is there a simple solution to this?

here is what I thought of using:

    char filename[64];      
          for (x=x; x<10; x=x) {    
            sprintf(filename, "/local/analog_000%d.txt", x);
            }
          for (x=x; 10<100; x=x) {
            sprintf(filename, "/local/analog_00%d.txt", x);
            }
          for (x=x; 100<1000; x=x) {
            sprintf(filename, "/local/analog_0%d.txt", x);
            }
          for (x=x; 1000<10000; x=x) {
            sprintf(filename, "/local/analog_%d.txt", x);
      FILE *fp = fopen(filename, "w");
But this is pretty ugly (plus it dosen't work, hangs up mbed on start-up)

any suggestions?

thanks

06 Aug 2010

%d gets replaced by the actual number passed to printf, not just one digit, so you're not "limited" to 10 files. If you want to insert additional zeroes, use the zero-padding modifier:

sprintf(filename, "/local/analog_%05d.txt", x);

This will pad the decimal value with zeroes if it's less than 5 digits long.

P.S. your for() operators are wrong, that's why mbed hangs (it actually enters infinite loop). You probably want something like this:

for ( x=1; x<100; x++)

06 Aug 2010

yea, I was improvising, I don't want the for operation to change the value of x.

I know, the naming scheme looks sound, you'd think after 9 it would just make a file called 10, but it doesn't, it writes 10 file from 0-9 then stops, the rest of the program runs, and even records data, but the 10 original files stay, and no more are added. x is just some integer than I have increment after data has been written to the file and the file closed.

06 Aug 2010 . Edited: 06 Aug 2010

Isn't there an 8.3 filename limit on the mbed?

Try changing your filename to something shorter - at the moment "analog_" uses 7 of the 8 characters, so you've only got one left for your number.

How about:

 sprintf(filename, "/local/an_%d.txt", x);
06 Aug 2010

thats it! thanks man, I was stuck on that one.