C 如何在目录和子目录中递归查找文件
到目前为止,我在Stack Overflow上找到了这个文件,但它只显示程序中给定的路径中的一个文件,而不是用户提供的 我尝试向C 如何在目录和子目录中递归查找文件,c,find,C,Find,到目前为止,我在Stack Overflow上找到了这个文件,但它只显示程序中给定的路径中的一个文件,而不是用户提供的 我尝试向printfile函数添加变量,但没有效果 int findfile_recursive(const char *folder, const char *filename, char *fullpath ) { char wildcard[MAX_PATH]; sprintf(wildcard, "%s\\*", folder); WIN32_F
printfile
函数添加变量,但没有效果
int findfile_recursive(const char *folder, const char *filename, char *fullpath )
{
char wildcard[MAX_PATH];
sprintf(wildcard, "%s\\*", folder);
WIN32_FIND_DATA fd;
HANDLE handle = FindFirstFile(wildcard, &fd);
if(handle == INVALID_HANDLE_VALUE) return 0;
do
{
if(strcmp(fd.cFileName, ".") == 0 || strcmp(fd.cFileName, "..") == 0)
continue;
char path[MAX_PATH];
sprintf(path, "%s\\%s", folder, fd.cFileName);
if(_stricmp(fd.cFileName, filename) == 0)
strcpy(fullpath, path);
else if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
findfile_recursive(path, filename, fullpath);
if(strlen(fullpath))
break;
} while(FindNextFile(handle, &fd));
FindClose(handle);
return strlen(fullpath);
}
int printfile(void)
{
char a,b;
printf("Path: ");
gets(a);
printf("Name: ");
gets(b);
char fullpath[MAX_PATH] = { 0 };
if(findfile_recursive(&a, &b, fullpath)){
printf("found: %s\n", fullpath);
}
else{
printf("Nothing found");
}
}
gets
返回了一个指向char
的指针,但您使用的是char
,另外请注意gets
已从标准中删除,您应该将其替换为fgets
,并去掉函数留下的尾随换行符
char str[1024]; // Space enough to store a path
printf("Path: ");
if (fgets(str, sizeof str, stdin)) // read stdin
{
char *ptr = strchr(str, '\n'); // Find the trailing newline
if (ptr != NULL)
{
*ptr = '\0'; // Strip the trailing newline
}
}
更正后,此代码执行其工作
int findfile_recursive(const char *folder, const char *filename, char *fullpath )
{
char wildcard[MAX_PATH];
sprintf(wildcard, "%s\\*", folder);
WIN32_FIND_DATA fd;
HANDLE handle = FindFirstFile(wildcard, &fd);
if(handle == INVALID_HANDLE_VALUE) return 0;
do
{
if(strcmp(fd.cFileName, ".") == 0 || strcmp(fd.cFileName, "..") == 0)
continue;
char path[MAX_PATH];
sprintf(path, "%s\\%s", folder, fd.cFileName);
if(_stricmp(fd.cFileName, filename) == 0)
strcpy(fullpath, path);
else if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
findfile_recursive(path, filename, fullpath);
if(strlen(fullpath))
break;
} while(FindNextFile(handle, &fd));
FindClose(handle);
return strlen(fullpath);
}
int printfile(void)
{
char name[1024], path[1024];
printf("\n\nFilename: ");
scanf("%s",name);
printf("Path: ");
scanf("%s",path);
char fullpath[MAX_PATH] = { 0 };
if(findfile_recursive(&path,&name, fullpath)){
printf("Found: %s\n", fullpath);
}
else{
printf("Nothing found\n");
}
}
首先,永远不要使用
get
,它是一个甚至已经从C规范中删除的函数。使用例如(但请注意get
和fgets
之间的区别以及它们在您提供的缓冲区中的位置)。其次,get
和fgets
都将指向字符数组第一个元素的指针作为参数。如何在单个字符中设置路径?编译器没有警告您将错误的参数传递给get
?调用findfile\u recursive
时需要使用运算符的地址,这应该表明您做错了什么。不,编译器没有显示任何错误。好的,我已经编辑了代码,现在变量可以匹配路径或文件名。请不要“修复”问题中的代码,尤其是在得到答案后。通过“修复”代码,您的问题不再有问题,或者至少不再有相同的问题,因此现在对其他人没有任何用处。谢谢,我刚刚编辑了我的代码。
int findfile_recursive(const char *folder, const char *filename, char *fullpath )
{
char wildcard[MAX_PATH];
sprintf(wildcard, "%s\\*", folder);
WIN32_FIND_DATA fd;
HANDLE handle = FindFirstFile(wildcard, &fd);
if(handle == INVALID_HANDLE_VALUE) return 0;
do
{
if(strcmp(fd.cFileName, ".") == 0 || strcmp(fd.cFileName, "..") == 0)
continue;
char path[MAX_PATH];
sprintf(path, "%s\\%s", folder, fd.cFileName);
if(_stricmp(fd.cFileName, filename) == 0)
strcpy(fullpath, path);
else if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
findfile_recursive(path, filename, fullpath);
if(strlen(fullpath))
break;
} while(FindNextFile(handle, &fd));
FindClose(handle);
return strlen(fullpath);
}
int printfile(void)
{
char name[1024], path[1024];
printf("\n\nFilename: ");
scanf("%s",name);
printf("Path: ");
scanf("%s",path);
char fullpath[MAX_PATH] = { 0 };
if(findfile_recursive(&path,&name, fullpath)){
printf("Found: %s\n", fullpath);
}
else{
printf("Nothing found\n");
}
}