我正在使用Windows API,并希望能够搜索指定的目录并返回其中的任何文件的名称。
我已经开始了,但是我已经碰到了一堵砖墙,因为我不确定如何继续前进。
到目前为止,我的进展如下:
#include <stdio.h>
#include <windows.h>
void main()
{
HANDLE fileHandle;
WIN32_FIND_DATAA fileData;
fileHandle = FindFirstFileA("*.txt", &fileData);
if(fileHandle != INVALID_HANDLE_VALUE)
{
printf("%s \n", fileData.cFileName);
}
}
答案 0 :(得分:2)
您需要在循环中调用FindNextFile以查找所有文件。有一个完整的例子here,这里有一些有趣的内容:
hFind = FindFirstFile(szDir, &ffd);
if (INVALID_HANDLE_VALUE == hFind)
return dwError;
do
{
printf("%s\n"), ffd.cFileName);
}
while (FindNextFile(hFind, &ffd) != 0);
答案 1 :(得分:0)
#include <stdio.h>
#include <windows.h>
void main()
{
HANDLE fileHandle;
WIN32_FIND_DATA ffd;
LARGE_INTEGER szDir;
WIN32_FIND_DATA fileData;
fileHandle = FindFirstFile("C:\\Users\\rest_of_the_Address\\*", &ffd);
if (INVALID_HANDLE_VALUE == fileHandle)
printf("Invalid File Handle Value \n");
do
{
printf("%s\n", ffd.cFileName);
} while (FindNextFile(fileHandle, &ffd) != 0);
system("pause");
}
You were missing some declarations, and had some syntax errors, fixed up here, and also, remember to check the msdn documentation (here is a msdn example for the program)