我正在尝试用 C++17 中的 filesystem 库遍历所有文件
但我遇到了问题:
#include <filesystem>
#include <iostream>
#include <string.h>
#include <comdef.h>
#include <string>
#include <windows.h>
using std::string;
using std::wstring;
std::wstring String2Wstring(std::string wstr)
{
std::wstring res;
int len = MultiByteToWideChar(CP_ACP, 0, wstr.c_str(), wstr.size(), nullptr, 0);
if( len < 0 ){
return res;
}
wchar_t* buffer = new wchar_t[len + 1];
if( buffer == nullptr){
return res;
}
MultiByteToWideChar(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len);
buffer[len] = '\0';
res.append(buffer);
delete[] buffer;
return res;
}
void FindFileEx_Cpp17(std::wstring pagthd) {
string paths,name;
try{
auto er=std::error_code();
for (auto &v:std::filesystem::recursive_directory_iterator(pagthd,std::filesystem::directory_options::skip_permission_denied,er))
{
if(er)
{
continue;
}
paths=v.path().string();
name=v.path().filename().string();
std::filesystem::path pathByfsp(paths);
if(v.is_regular_file())
{
std::cout<<paths<<std::endl;
}
else{
FindFileEx_Cpp17(pathByfsp);
}
}}
catch (std::exception& e)
{
std::cout<<e.what()<<std::endl;
}
}
bool getstart() {
DWORD bufferLength = GetLogicalDriveStrings(0, nullptr);
if (bufferLength == 0) {
std::wcerr << L"Failed to get logical drive strings length." << std::endl;
return false;
}
std::vector<wchar_t> buffer(bufferLength);
DWORD result = GetLogicalDriveStringsW(bufferLength, buffer.data());
if (result == 0 || result > bufferLength) {
std::wcerr << L"Failed to get logical drive strings." << std::endl;
return false;
}
wchar_t* drive = buffer.data();
while (*drive) {
std::wstring temp=drive;
std::wcout<<temp<<std::endl;
FindFileEx_Cpp17(temp);
drive += wcslen(drive) + 1;
}
return true;
}
int main()
{
system("chcp 65001 > nul");
getstart();
}
当它遍历到 C:\Documents and Settings 目录时抛出了一个问题:filesystem error: cannot increment recursive directory iterator: Invalid argument
我该怎么做