我需要建立一個文件路徑。我有以下類方法:在C++中使用cstrings建立路徑的正確方法
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
建立路徑的文件。當我嘗試運行這段代碼時,它會出現段錯誤。從我可以告訴它在我建立路徑的地方進行分割。有沒有更好的方法來做到這一點?