[英]C++ recursive_directory_iterator miss some files
I'm trying to get all files in directory through c++17 on my visual studio 2017 but I've just encountered a really weird problem. 我正在尝试通过Visual Studio 2017上的c ++ 17获取目录中的所有文件,但我刚遇到一个非常奇怪的问题。 If I specify directory like this I can get all files without any problem: 如果我这样指定目录,则可以毫无问题地获取所有文件:
for (auto& p : std::filesystem::recursive_directory_iterator("C:\\Users\\r00t\\AppData\\Roaming\\Mozilla")) {
if (std::filesystem::is_regular_file(p.path())) {
std::cout << p.path() << std::endl;
}
}
But I need all file list on APPDATA, and I'm trying to get path with getenv() function and when using it "recursive_directory_iterator" function skipping files: 但是我需要APPDATA上的所有文件列表,并且我尝试使用getenv()函数获取路径,并且在使用它时,“ recursive_directory_iterator”函数会跳过文件:
for (auto& p : std::filesystem::recursive_directory_iterator(getenv("APPDATA"))) {
if (std::filesystem::is_regular_file(p.path())) {
std::cout << p.path() << std::endl;
}
}
Is that because of using getenv() function? 那是因为使用了getenv()函数吗? Some folders that skipping when using getenv; 一些使用getenv时会跳过的文件夹;
Mozilla
TeamWiever
NVIDIA
and so on .. 等等 ..
Btw, I'm using C++ last 5 days and definitely don't have any clue what causes for that behavior. 顺便说一句,我最近5天一直在使用C ++,并且绝对不知道导致这种现象的原因。 Please help me, right now I'm stuck. 请帮助我,现在我被困住了。
EDIT : 编辑:
for (auto& p : std::filesystem::directory_iterator(getenv("APPDATA"))) {
std::string targetFolder = p.path().string();
for (auto& targetFolderFiles : std::filesystem::recursive_directory_iterator(targetFolder)) {
if (std::filesystem::is_regular_file(targetFolderFiles.path())) {
std::cout << targetFolderFiles.path() << std::endl;
}
}
}
This is also not working, seems like i must put string into function like this: 这也不起作用,似乎我必须将字符串放入这样的函数中:
recursive_directory_iterator("C:\\Users\\r00t\\AppData\\Roaming\\Mozilla")
otherwise definitely not working, LOL ?? 否则肯定无法正常工作,大声笑?
EDIT - PROBLEM FIXED 编辑-问题已解决
Using experimental library is working with C++14 compiler like as expected. 如预期的那样,使用实验库可与C ++ 14编译器一起使用。
#include <experimental/filesystem>
Now i can able to get all files without problem.Seems like this is problem about C++17 and filesystem library .. Thanks for all support guys. 现在我可以毫无问题地获取所有文件了。似乎这是关于C ++ 17和文件系统库的问题..感谢所有支持人员。
getenv()
returns a char*
or NULL
. getenv()
返回char*
或NULL
。 <filesystem>
is probably operating with wchar_t*
strings since you are on Windows. 由于您在Windows上, <filesystem>
可能与wchar_t*
字符串一起运行。 Use SHGetKnownFolderPath(...)
to query for where special folders are. 使用SHGetKnownFolderPath(...)
查询特殊文件夹的位置。
What happens when you run your program is probably that you hit some character that can't be displayed with your current locale ("C" if not set explicitly) so it sets your outstream in fail mode. 运行程序时发生的情况可能是您击中了某些无法在当前语言环境下显示的字符(如果未明确设置,则为“ C”),因此将您的流媒体设置为失败模式。 You can however set your locale to UTF-16LE to remedy this. 但是,您可以将语言环境设置为UTF-16LE来解决此问题。 It works with /std:c++17 and the standard <filesystem>
header: 它与/ std:c ++ 17和标准的<filesystem>
标头一起使用:
#include <Shlobj.h> // SHGetKnownFolderPath
#include <clocale> // std::setlocale
#include <io.h> // _setmode
#include <fcntl.h> // _O_U16TEXT
const char CP_UTF_16LE[] = ".1200";
setlocale(LC_ALL, CP_UTF_16LE);
_setmode(_fileno(stdout), _O_U16TEXT);
With that in place, the path you get from SHGetKnownFolderPath
should work: 有了这个,从SHGetKnownFolderPath
获得的路径应该可以正常工作:
PWSTR the_path;
if(SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_DEFAULT, NULL, &the_path) == S_OK) {
for(auto& p : std::filesystem::recursive_directory_iterator(the_path)) {
std::wcout << p.path() << L"\n";
// you can also detect if the outstream is in fail mode:
if (std::wcout.fail()) {
std::wcout.clear(); // ... and clear the fail mode
std::wcout << L" (wcout was fail mode)\n";
}
}
CoTaskMemFree(the_path);
}
You may also find the list of Default Known Folders in Windows useful. 您可能还会发现Windows中的“ 默认已知文件夹 ”列表很有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.