简体   繁体   中英

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. What am I doing wrong?

char and wchar_t are incompatible. You can't convert from/to automatically at the C++ level, you have to use instead a function such as WideCharToMultiByte .

Just forget 8-bit strings in Windows. Always use unicode, std::wstring , wchar_t , wfopen , etc. Windows internally uses 16-bit wide chars. UTF-8 is not automatically converted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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