简体   繁体   English

Windows 非 ASCII 文件路径

[英]Windows non-ASCII file paths

I am doing some experiment to a better understanding of conversion and have following code which is not working as expected我正在做一些实验以更好地理解转换,并且下面的代码没有按预期工作

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);
 }

I am expected that after converting wstring to string file open/fopen should work but it is not working.我预计在将 wstring 转换为字符串文件后 open/fopen 应该可以工作,但它不工作。 What am I doing wrong?我究竟做错了什么?

char and wchar_t are incompatible. charwchar_t不兼容。 You can't convert from/to automatically at the C++ level, you have to use instead a function such as WideCharToMultiByte .您不能在 C++ 级别自动转换/转换,您必须使用 function 代替,例如WideCharToMultiByte

Just forget 8-bit strings in Windows.只需忘记 Windows 中的 8 位字符串。 Always use unicode, std::wstring , wchar_t , wfopen , etc. Windows internally uses 16-bit wide chars.始终使用 unicode、 std::wstringwchar_twfopen等。Windows 内部使用 16 位宽的字符。 UTF-8 is not automatically converted. UTF-8 不会自动转换。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM