[英]Windows non-ASCII file paths
我正在做一些实验以更好地理解转换,并且下面的代码没有按预期工作
std::wstring inScriptPath = "non-ASCII file name"
using convert_type = std::codecvt_utf8_utf16<wchar_t>;
std::wstring_convert<convert_type, wchar_t> converter;
std::string my_path = converter.to_bytes(inScriptPath);
std::fstream myfile;
myfile.open(my_path.c_str(), ios::in); //open the file
if (myfile.is_open()) {
std::cout << "Openning file for reading with fstream" << std::endl;
myfile.close();
}
FILE * pFile;
pFile = fopen(my_path.c_str(), "r");
if (pFile != NULL)
{
std::cout << "Openning file for reading with fopen" << std::endl;
fclose(pFile);
}
我预计在将 wstring 转换为字符串文件后 open/fopen 应该可以工作,但它不工作。 我究竟做错了什么?
char
和wchar_t
不兼容。 您不能在 C++ 级别自动转换/转换,您必须使用 function 代替,例如WideCharToMultiByte 。
只需忘记 Windows 中的 8 位字符串。 始终使用 unicode、 std::wstring
、 wchar_t
、 wfopen
等。Windows 内部使用 16 位宽的字符。 UTF-8 不会自动转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.