gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 05:54:08 26 4
gpt4 key购买 nike

我在使用 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:

唯一会影响 pathopendir()这里。它有我没有看到的副作用吗?或者还有其他事情在起作用?

最佳答案

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

DIR *opendir(const char *dirname);

constopendir()没有改变它。

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

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

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