c++ - 在特定的 Unicode 路径中创建文件

标签 c++ windows file unicode widechar

我想用给定的位置路径创建一个包含 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/

相关文章:

c++ - 将数据转换为大端

c++ - 如何使用libpqxx正确选择日期字段?

c++ - 为什么这个 Windows 命令行程序不能将其标准输出重定向到文件?

node.js - 为什么我在使用 npm install -g package 时得到 Unexpected token '\u0000'

linux - Linux中的 ".filename"是什么以及如何打开该文件?

c++ - 从 C/C++ 到 NASM src

c++ - 套接字聊天系统 - 将它们广播给所有客户端

Ruby bundle 打开,设置 $EDITOR

iphone - 如何在 iPhone 中查找 .pdf 或 .txt 或文件夹?

c# - 如何使用 .NET 压缩目录?