我在使用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);
并且const
说opendir()
没有改变它。
我想知道您的路径
是否是指向已释放内存的指针?在这种情况下,内存可能已分配给 opendir()
并且您看到了更改,因为您使用的是指向内存的悬空指针,而您不应该查看该指针?
关于opendir(pathname) 可以更改输入路径名的值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9730957/