#168706 - Echo49 - Sat May 16, 2009 11:05 am
I'm trying to write some code to scan an entire directory which should be full of subdirectories, and inside those subdirectories are the files I want to have picked up by my code.
However, this code is failing at the first S_ISDIR statement. Both files and directories are returning 0 when testing with S_ISDIR. I've checked the value of st_mode and it's always 0, with both nitrofs on No$gba and libfat on hardware. Am I calling stat() incorrectly? I pretty much copypasted from the example.
However, this code is failing at the first S_ISDIR statement. Both files and directories are returning 0 when testing with S_ISDIR. I've checked the value of st_mode and it's always 0, with both nitrofs on No$gba and libfat on hardware. Am I calling stat() incorrectly? I pretty much copypasted from the example.
Code: |
void readDir()
{ chdir(/*some directory*/); struct stat fileinfo; DIR* dir = opendir("."); struct dirent* entry; while ((entry = readdir(dir)) != NULL) { //ignore generic names if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue; //get file info stat(entry->d_name, &fileinfo); //if this is a folder, find all the files inside if (S_ISDIR(fileinfo.st_mode)) { DIR* subdir = opendir(entry->d_name); struct dirent* subentry; while ((subentry = readdir(subdir)) != NULL) { //reuse stat struct stat(subentry->d_name, &fileinfo); //if this is a file if (!S_ISDIR(fileinfo.st_mode)) { char* ext = subentry->d_name; int length = strlen(ext); if (length < 4) continue; //ext is now the extension of the current file ext += length - 4; if (strcmp(ext, ".ext") == 0) ;//do stuff with this data } } closedir(subdir); } } closedir(dir); } |