opendir(pathname) 可以更改输入路径名的值吗?

标签 c directory-structure opendir

我在使用opendir()时收到了一些奇怪的结果:

int dtw(char *path) {

    struct stat statbuf;

    ...

    else if (S_ISDIR(statbuf.st_mode)) {
            printf("Path is: %s\n", path);

            struct dirent *dirent;
            DIR *dirp;

            if ((dirp = opendir(path)) == NULL) {
                puts("Can't open directory.");
                return -1;
            }

            printf("Path is: %s\n", path);
    }

    ...
}

结果:

Path is: /home/.../etc
Path is:

唯一会影响path的是这里的opendir()。它是否有我没有看到的副作用?还是还有其他事情在起作用?

最佳答案

不允许更改; opendir()的定义是:

DIR *opendir(const char *dirname);

并且constopendir()没有改变它。

我想知道您的路径是否是指向已释放内存的指针?在这种情况下,内存可能已分配给 opendir() 并且您看到了更改,因为您使用的是指向内存的悬空指针,而您不应该查看该指针?

关于opendir(pathname) 可以更改输入路径名的值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9730957/

相关文章:

c - 汇编程序和字对齐

c - Makefile 使用单个 make 文件构建两个源代码

linux - 提取 zip 文件内容并动态重命名目录

php - 使用 PHP RecursiveTreeIterator 构建日志读取器

PHP:删除目录的安全方法?

php - 用php忽略隐藏文件

c - C中的目录问题

c - 给定中序和前序遍历生成后序遍历

c - 使用 char 指针类型转换的 int 指针抛出意外输出?

react-native - 制作项目结构的最佳实践 - React Native