简体   繁体   English

如何将const std :: filesystem :: directory_entry转换为tchar?

[英]How to convert const std::filesystem::directory_entry to tchar?

I'm trying to convert const std::filesystem::directory_entry (dirent) to tchar but I don't understand how it an be done. 我正在尝试将const std :: filesystem :: directory_entry(dirent)转换为tchar,但我不知道该怎么做。 I tried a lot of ways. 我尝试了很多方法。 Can you help me? 你能帮助我吗?

Edited: 编辑:

 #include "pch.h"
    #include <memory>
    #include <filesystem>
    #include <algorithm>
    #include <iostream>
    #include <tchar.h>
    #include <string.h>
    typedef wchar_t WCHAR;
    namespace fs = std::filesystem;

    int main() try
    {
        std::for_each(fs::recursive_directory_iterator("./foo/"), {},
            [](fs::directory_entry const& dirent)
        {

                if (fs::is_regular_file(dirent) &&
                    dirent.path().filename() == "black.txt")
                {
                    std::wstring = path.wstring();
                }


        });
    }
    catch (fs::filesystem_error const& e)
    {
        std::cerr << "error: " << e.what() << '\n';
    }

You can convert std::filesystem::path to any form using these functions : 您可以使用以下功能std::filesystem::path转换为任何形式:

std::string string() const;
std::wstring wstring() const;
std::u16string u16string() const;
std::u32string u32string() const;

TCHAR is somewhat of a relic as mentioned in the comments, and you're much better off using any of the above alternatives. 如评论中所述, TCHAR有点像遗物,而使用上述任何一种替代方法,您的状况都会更好。

If you're going to pass it to Win32 API functions, I would either suggest using wstring explicitly and not bother with the shape-shifting TCHAR at all. 如果要将其传递给Win32 API函数,我要么建议显式使用wstring ,要么完全wstring会变形的TCHAR


What your code should look like is this: 您的代码应如下所示:

if (fs::is_regular_file(dirent) &&
    dirent.path().filename() == "black.txt")
{    
    std::wstring path_as_string = path.wstring();
}

No TCHAR , no C-style arrays no C-style memory copying. 没有TCHAR ,没有C样式的数组,没有C样式的内存复制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 const std::filesystem::directory_entry&amp; 转换为 c++ 中的字符串 - How to convert const std::filesystem::directory_entry& to string in c++ C++ 递归地遍历路径。 错误:未找到采用“const std::filesystem::directory_entry”类型的右手操作数的运算符 - C++ iterate through a path recursively. Error: no operator found which takes a right-hand operand of type 'const std::filesystem::directory_entry' 如何将 std::wstring 转换为 const TCHAR*? - How to convert std::wstring to a const TCHAR*? 将std :: string转换为const tchar * - convert std::string to const tchar* 如何为字符串分配boost :: filesystem :: directory_entry :: path()值? - How can I assign boost::filesystem::directory_entry::path() value to a string? 如何将“指向const TCHAR的指针”转换为“std :: string”? - How do I convert a “pointer to const TCHAR” to a “std::string”? 怎么把TCHAR转换成const char? - How to convert TCHAR to const char? 如何将 std::wstring 转换为 TCHAR*? - How to convert std::wstring to a TCHAR*? 正确的方式crossplatfom从std :: string转换为&#39;const TCHAR *&#39; - Proper way crossplatfom convert from std::string to 'const TCHAR *' 如何将 TCHAR 数组转换为 std::string? - How to convert a TCHAR array to std::string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM