[英]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.