fopen question

27 Mar 2011

Is there a reason why you can't use a variable as the file path parameter in fopen? I replace fname with "/local/test.txt" it works fine.

#include "mbed.h"

LocalFileSystem local("local");

void filename(char * fname) {
    char datestamp[10];
    time_t seconds = time(NULL);
    strftime(datestamp, 6, "%m_%d", localtime(&seconds));
    strcpy(fname,"/local/"); // directory
    strcpy(fname,datestamp);
    strcat(fname,".txt");
}

void writelog() {
    char fname[20]; // will be in the form 8.3  MM_DD.txt
    filename(fname);
    printf("Filename is: %s\r\n",fname);
    FILE *fp = fopen(fname, "w");
    if (!fp) {
        fprintf(stderr, "File '%s' could not be opened! (ERR: %d)\r\n",fname,fp);
        exit(1);
    }
    wait(5.0);
    fprintf(fp, "Hello World!");
    wait(5.0);
    fclose(fp);
}

int main() {
    printf("Starting.\r\n");
    set_time(1301192486);
    writelog();
    while (1){
    }
}

The results:

Starting.
Filename is: 03_27.txt
File '03_27.txt' could not be opened! (ERR: 0)

Thanks, David

27 Mar 2011

You need the /local/ at the beginning of the path. Use strcat instead of strcpy when adding the date stamp portion:

    strcpy(fname,"/local/"); // directory
    strcat(fname,datestamp);
    strcat(fname,".txt");
27 Mar 2011

Well I found a way to make it work. I replaced the strcpy and strcat functions with an sprintf and we have joy. Can someone explain why the original method didn't work??

David

#include "mbed.h"

LocalFileSystem local("local");

void filename(char * fname) {
    char datestamp[10];
    time_t seconds = time(NULL);
    strftime(datestamp, 6, "%m_%d", localtime(&seconds));
    sprintf(fname, "/local/%s.txt", datestamp);

}

void writelog() {
    char fname[20]; // will be in the form 8.3  MM_DD.txt
    filename(fname);
    printf("Filename is: %s\r\n",fname);
    FILE *fp = fopen(fname, "w");
    if (!fp) {
        fprintf(stderr, "File '%s' could not be opened! (ERR: %d)\r\n",fname,fp);
        exit(1);
    }
    wait(5.0);
    fprintf(fp, "Hello World!");
    wait(5.0);
    fclose(fp);
}

int main() {
    printf("Starting.\r\n");
    set_time(1301192486);
    writelog();
    while (1) {
    }
}
27 Mar 2011

fname = "03_27.txt" ? I thought you need the full path, i.e /local/03_27.txt

27 Mar 2011

Ooops. Adam, I didn't see that strcpy - a cut/paste error. That, of course, fixes the problem. So now I'd like to ask which approach is best? Use sprintf or strcpy/strcat? It seems sprintf might be a bit overkill.

David

27 Mar 2011

I use strcat sparingly myself since it walks over the bytes at the beginning of the destination buffer to find the end of the current string it contains. This is walking over all of the bytes that you have already stored in previous strcpy()/strcat() calls but performance probably doesn't matter in your case. Personally I find the sprintf() method clearer when reading code since it shows you see the bulk of the string that you are building up in one place rather than having to piece together in your mind what the strcat() calls are doing:

sprintf(fname, "/local/%s.txt", datestamp);