如何使用Visual C++获取文件夹中的所有文件名
How can I get Get All Filenames in a Folder using Visual C++?
我试过这个代码,但没有得到任何输出。它也不会抛出错误。
#include <windows.h>
int main(int argc, char* argv[])
{
WIN32_FIND_DATA search_data;
memset(&search_data, 0, sizeof(WIN32_FIND_DATA));
HANDLE handle = FindFirstFile("c:\*.txt", &search_data);
while(handle != INVALID_HANDLE_VALUE)
{
printf("Found file: %srn", search_data.cFileName);
if(FindNextFile(handle, &search_data) == FALSE)
break;
}
return 0;
}`
//using boost file system.
#include <boost/filesystem.hpp>
#include <iostream>
int main()
{
using namespace boost::filesystem;
path myPath(L"D:\Article");
for (auto beg = directory_iterator(myPath); beg != directory_iterator(); ++beg) {
std::cout << *beg << std::endl;
}
}
您没有得到任何输出,因为FindFirstFile()
失败并且您忽略了错误。始终检查API错误代码。
试试这个。
#include <windows.h>
int main(int argc, char* argv[])
{
WIN32_FIND_DATA search_data;
DWORD err;
memset(&search_data, 0, sizeof(WIN32_FIND_DATA));
HANDLE handle = FindFirstFile("c:\*.txt", &search_data);
if (handle == INVALID_HANDLE_VALUE)
{
err = GetLastError();
if (err == ERROR_FILE_NOT_FOUND)
printf("No files were foundrn");
else
printf("Unable to search for files. Error: %urn", err);
}
else
{
do
{
if ((search_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
printf("Found file: %srn", search_data.cFileName);
}
while (FindNextFile(handle, &search_data));
err = GetLastError();
if (err == ERROR_NO_MORE_FILES)
printf("Donern");
else
printf("Unable to search for files. Error: %urn", err);
FindClose(handle);
}
return 0;
}
相关文章:
- 如何将更多文件夹添加到c++include路径
- GetShortPathName在网络驱动器上使用中文文件夹时失败
- VS2017,C++包含目录与附加包含目录,子文件夹包含失败-但为什么
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 如何在文档文件夹中创建目录
- "assimp/config.h"找不到,但它在文件夹中
- 如何在目录及其子文件夹中构建文件名字符串向量?
- 如何更改目录中文件夹中的文件名
- 如何从文件夹中获取文件名,并将它们添加到QT中的孩子中
- 在文件夹中将图像文件名编写到文本文件C 中
- 如何使用Visual C++获取文件夹中的所有文件名
- 写入 C:Program Files 中包含的文件夹的文件名
- 交换同一文件夹C++中的文件名
- 如何用c++将文件夹中的文件名存储到数组中
- 有没有一种方法可以在不引起竞争的情况下检查重复的文件夹/文件名
- 检查文件夹中是否已经存在文件名
- 如何在c++中将用户指定的文件名从一个文件夹移动到另一个文件夹
- 获取用户计算机上文件名按文本行筛选的文件和文件夹列表
- 如何从文件夹中读取文件?文件夹包含不同类型的文件名
- 从文件夹C++中获取文件名