FileSystem - Storing root names

05 May 2011

I have written an audio synth and would like to be able to save patches. I can successfully store all my variables to a predefined filename and recall from that filename but i would also like to be able to store and recall several patches under different filenames. As part of this I need to list the files stored on an SD card. A bit of searching turned up this post http://mbed.org/forum/helloworld/topic/1516/?page=1#comment-7511 and with a little bit of adjustment I can display all files with a .sav extension (my patch files).

#include "mbed.h"
#include "SDFileSystem.h"

LocalFileSystem local("local");
SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board

int main() {

    printf("List of files in /sd/mydir\n");

    DIR *d;
    struct dirent *p;
    char array[256];
    d = opendir("/sd/mydir");
    if (d != NULL) {
        while ((p = readdir(d)) != NULL) {
            if ((strstr(p->d_name,".SAV"))||(strstr(p->d_name,".sav"))) {
                printf(" - %s\n\r", p->d_name);
            }
        }
    } else {
        error("Could not open directory!");
    }
    printf("%s\n\r",array);
}

The problem now is that I can't figure out how to store the filename strings returned. Am I right in thinking that strings are not supported? I tried the following code: -

#include "mbed.h"
#include "SDFileSystem.h"

LocalFileSystem local("local");
SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board

int main() {

    printf("List of files in /sd/mydir\n");

    DIR *d;
    struct dirent *p;
    char array[256];
    d = opendir("/sd/mydir");
    if(d != NULL) {
        if((p = readdir(d)) != NULL) {
            string tmp;
            tmp = p->d_name;
            printf(" - %s\n\r", tmp);
        }
    } else {
        error("Could not open directory!");
    }
    printf("%s\n\r",array);
}

But the compiler returns the error: -

Quote:

"Identifier "string" is undefined (E20) <a href="#" onClick="window.open('/cookbook/Compiler-Error-20');">(more info)</a>" in file "/main.cpp"

Any ideas if this is achievable?

James

05 May 2011

Trying including the string header so that the C++ compiler knows about the string class.

#include "mbed.h"
#include "SDFileSystem.h"
#include <string>

LocalFileSystem local("local");
SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board

int main() {

    // ... commenting out code except for the chunk you care about
        if((p = readdir(d)) != NULL) {
            string tmp;
            tmp = p->d_name;
            printf(" - %s\n\r", tmp.c_str());  // Added call to c_str() method
        }
{
05 May 2011

Thanks Adam, works great.

James