我需要建立一个文件的路径。我有以下类方法:
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);
}
}
我需要通过连接目录和entry->d_name
. 当我尝试运行此代码时,它会出现段错误。据我所知,它在我构建路径的地方出现了段错误。有没有更好的方法来做到这一点?