【发布时间】:2017-04-10 17:37:11
【问题描述】:
我从here找到并修改了解决方案:
#include <iostream>
#include <windows.h>
#include <vector>
#include <fstream>
using namespace std;
//wofstream out;
void FindFile(const std::wstring &directory)
{
std::wcout << endl << endl << endl << "FindFile(" << directory << ")" << std::endl;
std::wstring tmp = directory + L"\\*";
WIN32_FIND_DATAW file;
HANDLE search_handle = FindFirstFileW(tmp.c_str(), &file);
if (search_handle != INVALID_HANDLE_VALUE)
{
std::vector<std::wstring> directories;
do
{
std::wcout << std::endl;
std::wcout << " [" << file.cFileName << "]" << std::endl;
tmp = directory + L"\\" + std::wstring(file.cFileName);
if (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if ((!lstrcmpW(file.cFileName, L".")) || (!lstrcmpW(file.cFileName, L".."))) {
std::wcout << "continuing..." << std::endl;
}
else {
std::wcout << "saving path to this directory" << std::endl;
directories.push_back(tmp);
}
} else {
std::wcout << "save [" << tmp << "] as file" << std::endl;
}
//std::wcout << tmp << std::endl;
//out << tmp << std::endl;
}
while (FindNextFileW(search_handle, &file));
std::wcout << "all items inside current directory was worked out. close it's handle." << std::endl;
FindClose(search_handle);
for(std::vector<std::wstring>::iterator iter = directories.begin(), end = directories.end(); iter != end; ++iter) {
std::wcout << "recursively find in next directory: [" << *iter << "]" << std::endl;
FindFile(*iter);
}
} else {
std::wcout << "invalid handle value" << std::endl;
}
}
int main()
{
//out.open("C:\\temp\\found.txt");
FindFile(L"C:\\test");
//out.close();
cout << "The end" << endl;
string str;
cin >> str;
return 0;
}
但此代码不适用于具有西里尔名称的文件夹或文件(但我使用所有类型和功能的 Unicode 版本!)
更新:应用程序刚刚完成,没有任何异常,好像所有命令都已执行。
谁有同样的问题?感谢您的帮助。
已解决 非常感谢@zett42! 经过一些重构工作代码看起来像:
#include <iostream>
#include <windows.h>
#include <vector>
#include <fstream>
#include <io.h>
#include <fcntl.h>
using namespace std;
vector<wstring> FindFiles(const std::wstring &directory) {
vector<wstring> files;
std::vector<std::wstring> directories;
std::wstring fullPath = directory + L"\\*";
WIN32_FIND_DATAW file;
HANDLE search_handle = FindFirstFileW(fullPath.c_str(), &file);
if (search_handle == INVALID_HANDLE_VALUE)
return files;
do
{
fullPath = directory + L"\\" + std::wstring(file.cFileName);
if (!(file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
files.push_back(fullPath);
else {
if ((lstrcmpW(file.cFileName, L".")) && (lstrcmpW(file.cFileName, L"..")))
directories.push_back(fullPath);
}
}
while (FindNextFileW(search_handle, &file));
FindClose(search_handle);
for(std::vector<std::wstring>::iterator iter = directories.begin(), end = directories.end(); iter != end; ++iter) {
vector<wstring> newFiles = FindFiles(*iter);
files.insert(files.begin(), newFiles.begin(), newFiles.end());
}
return files;
}
int main()
{
_setmode( _fileno(stdout), _O_U16TEXT );
vector<wstring> files = FindFiles(L"E:\\test");
wcout << L"All found files: " << endl;
for (int i = 0; i < files.size(); ++i)
wcout << files[i] << endl;
cout << "The end" << endl;
string str;
cin >> str;
return 0;
}
【问题讨论】:
-
"...但是这段代码不起作用..." 你能解释一下吗?它会跳过它们吗?你看到它们的名字有错吗?程序崩溃了?
-
没有。应用程序刚刚完成,没有任何错误。 (谢谢,@RichardCritten)
-
为
FindFirstFileW和FindNextFileW添加一些错误检查,如果它们失败,则输出GetLastError的结果。还要调试您的程序并单步执行它 - 实际上是第一次调试。 -
std::wcout << "invalid handle value" << std::endl;和其他类似的行在我看来是错误的。 -
@RichardCritten:可能是这样,但调用了
const void*模板特化。无论如何,那些应该是宽字符串文字,例如wcout << L"save [" << ...;.
标签: c++ winapi recursion unicode