I need to build up a path to a file. I have the following class method:
void Directory::scanDirectory(char *directory) {
DIR *dirp;
struct dirent *entry;
char path[1];
if(dirp = opendir(directory)) {
while(entry = readdir(dirp)) {
if (entry->d_name[0] != '.') {
strcpy(path, directory);
strcat(path, "/");
strcat(path, entry->d_name);
if (entry->d_type == 8) {
// Files
} else if (entry->d_type == 4) {
//scanDirectory(path);
}
printf("Name: %s, Type: %d\n", entry->d_name, entry->d_type);
}
}
closedir(dirp);
}
}
I need to build up the path to files by concatenating the directory and entry->d_name
. When I try running this code it segfaults. From what I can tell it's segfaulting at the point where I build the path. Is there a better way of doing this?