Wednesday, 15 September 2010

c - how to browse files in a folder? -



c - how to browse files in a folder? -

i'm trying browse files in folder compute figures, , ran problem trying so. i'm working code::blocks, , writing in c. i've looked @ similar questions & answers on website, none seemed adress issues.

here's how code begins :

#include <dirent.h> #define prod #ifndef prod #define dirpath "c:\\users\\user1\\...etc #define filepath "c:\\users\\user1\\.. \\" #else #define dirpath "c:\\users\\user2\\..etc #define filepath c:\\users\\user2\\.. \\" #endif int main(){ dir* dir = null; struct dirent *currentfile = null; dir = opendir(dirpath); char path[100] = filepath; while((currentfile = readdir(dir)) != null) { if(strcmp(currentfile->d_name, ".") != 0 && strcmp(currentfile->d_name, "..") != 0) { char *filepath = (char *) malloc((strlen(path) + strlen(currentfile->d_name) - 1) * sizeof(char)); filepath= strcpy(filepath, path); strcat(filepath,currentfile->d_name); file *file = fopen(filepath,"r"); // programme dies @ point // free(filepath); fclose(file); // rest runs fine } }

by using printf, able deduce programme crashes when it's supposed fopen file, although don't understand why. i've printed filepath, , gives me it's supposed to. can help me figure out problem ?

thanks lot, nicolas

c file folder

No comments:

Post a Comment