gpt4 book ai didi

c - while ... readdir 导致段错误

转载 作者:行者123 更新时间:2023-11-30 15:19:41 30 4
gpt4 key购买 nike

在 main 中,我使用 deletefolder() 调用以下函数:

void deletefolder(){

struct dirent *next_file;
DIR *folder;

char filepath[256];

folder = opendir("./game/wow");

while((next_file = readdir(folder)) != NULL){ //this is causing the segmentation fault. I don't know why?


sprintf(filepath, "%s/%s", "./game/wow", next_file->d_name);
remove(filepath);
}
}

我不明白为什么会发生这种情况?

最佳答案

while((next_file = readdir(folder)) != NULL){ //this is causing the segmentation fault. i dont know why??

我怀疑打开目录时出错。添加一些错误检查代码。

folder = opendir("./game/wow");
if ( folder == NULL )
{
perror("Unable to open folder.");
return;
}

while((next_file = readdir(folder)) != NULL){ ...

关于c - while ... readdir 导致段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30536449/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com