gpt4 book ai didi

c - 使用 readdir() 的段错误

转载 作者:太空狗 更新时间:2023-10-29 15:52:42 25 4
gpt4 key购买 nike

我刚刚开始使用 C 编程,我正在尝试读取和显示目录中的文件(就像 ls 命令一样)。

这是我遇到段错误的部分代码,我不知道为什么:

void    display_dir(char *dir)
{
DIR *strm;
struct dirent *direct;

if((strm = opendir(dir) == NULL))
{
printf("ERROR: Couldn't open directory.\n");
exit(1);
}
while ((direct = readdir(strm)) != NULL)
display_elems(direct);
closedir(strm);
}

经过一些测试,程序出现段错误时出现:

while ((direct = readdir(strm)) != NULL)

我做了一些研究,但没有找到任何可以帮助我的东西。

最佳答案

if((strm = opendir(dir) == NULL))

括号嵌套错误。应该是:

if((strm = opendir(dir)) == NULL)

关于c - 使用 readdir() 的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16499581/

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