我想用给定的位置路径创建一个包含 Unicode 字符的文件。我试过下面的代码,但在编译我的代码后没有生成文件。
wchar_t path = (wchar_t)"D:/File/ফোল্ডার/filename.txt";
std::wofstream file(&path); //open in constructor
std::wstring dat((wchar_t*)"Data to Write");
file << dat;
最佳答案
“D:/File/ফোল্ডার/filename.txt”
是 string literal类型为 const char [N]
。所以 (wchar_t)"D:/File/ফোল্ডার/filename.txt"
将一个 const char *
指针指向 wchar_t
,但这是行不通的你想要什么
除了wchar_t路径
声明一个单个宽字符而不是wchar_t的字符串
您需要使用L
前缀来获取wchar_t
的字符串文字,并改为声明一个指针:
const wchar_t* path = L"D:/File/ফোল্ডার/filename.txt";
std::wofstream file(path); //open in constructor
std::wstring dat(L"Data to Write");
file << dat;
另请注意,Windows 上的默认路径分隔符是反斜杠 \
而不是斜杠 /
,尽管对于许多 API,它们的行为相同。
关于c++ - 在特定的 Unicode 路径中创建文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59729948/