我正在编写一个函数 next_node
来查找目录中的下一个文件。节点将目录和文件名作为输入。
如果 bname
之后没有其他文件或者它是 "."
或 ",我希望它返回
。仅当它在 NULL
..”strcmp
的 if
语句中运行时,它才会给我 segmentation fault (core dumped)
。
能否请您解释一下问题或给出解决方案?
代码:
#include <stdio.h>
#include <dirent.h> // DIR opendir() closedir() struct dirent readdir()
#include <string.h> // strcmp()
char *next_node(char *dname, char *bname) {
if (!strcmp(dname, bname)) {
// dname same as bname
return NULL;
}
DIR *dirp = opendir(dname);
struct dirent *direntp;
while (((direntp = readdir(dirp)) != NULL) && (strcmp(direntp->d_name, bname))) {
}
if ((direntp = readdir(dirp)) != NULL) {
// if d_name is "." or ".." return NULL
if ((strcmp(direntp->d_name, ".")) || (strcmp(direntp->d_name, ".."))) {
return NULL;
}
// it can reach here with no problem
closedir(dirp);
return direntp->d_name;
} else {
closedir(dirp);
return NULL;
}
}
int main() {
char *dname = ".";
char *bname = "test.c";
char *result = next_node(dname, bname);
printf("%s\n", result);
return 0;
}
最佳答案
你有五个错误。
1:
DIR *dirp = opendir(dname);
您不检查此 opendir
是否成功。
2:
struct dirent *direntp;
while (((direntp = readdir(dirp)) != NULL) && (strcmp(direntp->d_name, bname))) {
}
if ((direntp = readdir(dirp)) != NULL) {
在这里,您调用了 readdir
,即使之前的循环因为 readdir
返回 NULL
而终止。你想要:
if ((direntp != NULL) && ((direntp = readdir(dirp)) != NULL)) {
3:
if ((strcmp(direntp->d_name, ".")) || (strcmp(direntp->d_name, ".."))){
将整数转换为 bool 值等同于询问它是否不为零。 strcmp
函数在匹配时返回零。所以问它是否不为零就是问它是否不匹配。但是一切都不是“。”的匹配项。或不匹配“..”!你想要:
if ((!strcmp(direntp->d_name, ".")) || (!strcmp(direntp->d_name, ".."))){
4:
// it can reach here with no problem
closedir(dirp);
return direntp->d_name;
您刚刚将指针返回到您关闭的目录中,使指针无效。您需要确定返回指针的生命周期应该是多长,并可能分配一些内存来返回。
也许:
char *ret = strdup (dirent->d_name);
closedir(dirp);
return ret;
请注意,调用方在使用完返回的字符串后需要释放
。
5:
char *result = next_node(dname, bname);
printf("%s\n", result);
如果 result
为 NULL
,这将失败。尝试:
char *result = next_node(dname, bname);
printf("%s\n", (result == NULL) ? "NULL" : result);
关于c - 段错误检查 strcmp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42308988/