因此,我正在进行目录遍历,并且无法使opendir按我所希望的方式工作。它总是无法打开我发送给它的目录,它会给出一些未知的错误。我通常通过argv1,但我放弃了,只是开始了硬编码路径。
char *dest = NULL;
char *filePath = ".";
char *newPath = NULL;
DIR *dirp;
struct dirent *dp;
int errno;
dirp = opendir("/home/cs429/Desktop/test");
struct stat path_stat;
if(dirp == NULL);
{
// Error Handling
fprintf(stderr, "Error failed to open input directory -%s\n",strerror(errno) );
return 1;
}有人知道我能做些什么来获得空值吗?我还在gdb:./sysdeps/posix/opendir.c:没有这样的文件或目录中得到了这一点。
发布于 2017-11-11 21:00:27
线
if(dirp == NULL);什么也不做,下一个代码块总是被执行,而不管dirp的值如何。
请删除;,以便代码是
if(dirp == NULL)
{
// Error Handling
fprintf(stderr, "Error failed to open input directory -%s\n",strerror(errno) );
return 1;
}发布于 2017-11-11 22:41:03
首先,检查给定路径是否确实存在。
dirp = opendir("/home/cs429/Desktop/test");手册页说:“opendir()函数返回一个指向direc-tory流的指针。
在这里,您将dirp与正确的NULL进行了比较,但是您在if语句之后插入了分号,这称为dummy if。也就是说,无论条件是否为真,立即语句都将始终执行。因此,在;之后删除if
if(dirp == NULL)
{
//some code
}https://stackoverflow.com/questions/47242665
复制相似问题