Pieter Hulshoff wrote:

For my front-end written in C++ I'm trying to figure out how to place the names of (music) files in a directory in a vector<string> variable. Any programmer on the list willing and able to help me with this one? I've been Googling for about an hour trying to find an answer, but no luck sofar.

See attached, but what look for the UNIX Programming FAQ - it discusses reading directories in good detail.

I pulled this from some WxWindows code I've written, so it may have a wx*
datatype or two.  I couldn't see any with a quick look, but the compiler
will be the final arbiter.

cheersbigears,
-kt



// given an (empty) vector of strings, fill it out with
// all files in the given <directory>
int getDirectory(string directory,vector<string> &ls)
{
    int rc=1;
     DIR *list;
    struct dirent *de;
    struct stat file;

    list = opendir(directory.c_str());
    if (list != NULL)
    {
        rewinddir(list);
        while ((de = readdir(list)))
        {
            if (strcmp(de->d_name,".") != 0 && strcmp(de->d_name,"..") != 0)
            {
                char *filename = new char 
[16+directory.size()+strlen(de->d_name)+strlen("/")];
                assert(filename != NULL);
                strcpy(filename,directory.c_str());
                strcat(filename,"/");
                strcat(filename,de->d_name);
                if (stat(filename,&file) == 0)
                {
                    string entry = filename;
                    if (S_ISREG(file.st_mode)) // is it's a regular file
                    {
                        ls.push_back(entry);
                    }
                    else if (S_ISDIR(file.st_mode)) // it's a directory
                    {
                        if (entry != "." && entry != "..")
                        {
                            //entry += '/';
                            ls.push_back(entry);
                        }
                    }
                }
                delete[] filename;
            }
        }
        closedir(list);
    }
    else
    {
        rc = 0;
    }
    // TODO add some more error handling
    return rc;
}


_______________________________________________ Xmame mailing list [EMAIL PROTECTED] http://toybox.twisted.org.uk/mailman/listinfo/xmame

Reply via email to