C ++ recursive_directory_iterator错过了一些文件 [英] C++ recursive_directory_iterator miss some files

查看:104
本文介绍了C ++ recursive_directory_iterator错过了一些文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Visual Studio 2017上通过c ++ 17获取目录中的所有文件,但我遇到了一个非常奇怪的问题.如果我这样指定目录,我可以毫无问题地获取所有文件:

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. 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;
        }
}

但是我需要APPDATA上的所有文件列表,并且我试图通过getenv()函数获取路径,并且在使用它时,"recursive_directory_iterator"函数会跳过文件:

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:

    for (auto& p : std::filesystem::recursive_directory_iterator(getenv("APPDATA"))) {
    if (std::filesystem::is_regular_file(p.path())) {
            std::cout << p.path() << std::endl;
        }
}

那是因为使用了getenv()函数吗?使用getenv时某些文件夹会跳过;

Is that because of using getenv() function? Some folders that skipping when using getenv;

Mozilla 
TeamWiever
NVIDIA  

以此类推..

顺便说一句,我最近5天一直在使用C ++,并且绝对不知道导致这种现象的原因.请帮助我,现在我被困住了.

Btw, I'm using C++ last 5 days and definitely don't have any clue what causes for that behavior. Please help me, right now I'm stuck.

    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 ??

编辑-问题已解决

使用实验库可以像预期的那样与C ++ 14编译器一起工作.

Using experimental library is working with C++14 compiler like as expected.

#include <experimental/filesystem>

现在我可以毫无问题地获取所有文件了.似乎这是关于C ++ 17和文件系统库的问题. 感谢所有支持人员.

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.

推荐答案

getenv()返回char*NULL.由于您在Windows上,<filesystem>可能 使用wchar_t*字符串进行操作.使用SHGetKnownFolderPath(...)查询特殊文件夹的位置.

getenv() returns a char* or NULL. <filesystem> is probably operating with wchar_t* strings since you are on Windows. Use SHGetKnownFolderPath(...) to query for where special folders are.

运行程序时发生的情况可能是您击中了一些无法在当前语言环境下显示的字符(如果未明确设置,则显示为"C"),因此将您的流媒体设置为失败模式.但是,您可以将语言环境设置为UTF-16LE来解决此问题.它可以与/std:c ++ 17和标准的<filesystem>标头一起使用:

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. You can however set your locale to UTF-16LE to remedy this. It works with /std:c++17 and the standard <filesystem> header:

#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

_setmode(_fileno(stdout), _O_U16TEXT);

有了这个,从SHGetKnownFolderPath获得的路径应该可以工作:

With that in place, the path you get from SHGetKnownFolderPath should work:

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);
}

您还可能会找到默认已知文件夹在Windows中有用.

You may also find the list of Default Known Folders in Windows useful.

这篇关于C ++ recursive_directory_iterator错过了一些文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆