c - 段错误检查 strcmp

标签 c segmentation-fault strcmp dirent.h

我正在编写一个函数 next_node 来查找目录中的下一个文件。节点将目录和文件名作为输入。

如果 bname 之后没有其他文件或者它是 "."",我希望它返回 NULL ..”。仅当它在 strcmpif 语句中运行时,它才会给我 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);

如果 resultNULL,这将失败。尝试:

char *result = next_node(dname, bname);
printf("%s\n", (result == NULL) ? "NULL" : result);

关于c - 段错误检查 strcmp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42308988/

相关文章:

C编程: Error in writing values of a particular column from a file to another file

c - 在列表中搜索节点既有效又无效

c++ - strcmp 从 ‘char’ 到 ‘const char*’ 的转换

iOS - 如何使用 OpenGLES 创建自定义 GPUImageFilters

c - 声明意外行为

c - 在结构中包含更改 sizeof in_addr

c - 学习C,[1]为什么要在main中获得无限循环? [2]为什么在不触摸文件检查代码的情况下出现段错误

c - C 中的隐式类型转换(将 32 位 unsigned in 转换为 8 位 u int)

c - 为什么我在写入文件时神秘地出现段错误?

c - 有趣的 strcmp 实现失败。 (C)