简体   繁体   中英

c++ fstreams open file with utf-16 name

At first I built my project on Linux and it was built around streams.

When I started moving to Windows I ran into some problems.
I have a name of the file that I want to open in UTF-16 encoding. 在此处输入图片说明

I try to do it using fstream:

QString source;  // content of  source  is shown on image
char *op= (char *) source.data();
fstream stream(op, std::ios::in | std::ios::binary);

But file cannot be opened.
When I check it,

 if(!stream.is_open())
   {}  // I always get that  it's not opened. But file indeed exists.

I tried to do it with wstream. But result is the same, because wstream accepts only char * too. As I understand it's so , because string , that is sent as char * , is truncated after the first zero and only one symbol of the file's name is sent, so file is never found. I know wfstream in Vissual studio can accept wchar_t * line as name, but compiler of my choice is MinGW and it doesn't have such signature for wstring constructor.

Is there any way to do it with STL streams?

ADDITION
That string can contaion not only Ascii symbols, it can contain Russian, German, Chinese symbols simultaneously. I don't want limit myself only to ASCII or local encoding.

NEXT ADDITION
Also data can be different, not only ASCII, otherwise I wouldn't bother myself with Unicode at all. Eg

在此处输入图片说明

Thanks in advance!

Boost :: Filesystem尤其是fstream.hpp标头可能会有所帮助。

If you are using MSVC and it's implementation of the c++ standard library, something like this should work:

QString source;  // content of  source  is shown on image
wchar_t *op= source.data();
fstream stream(op, std::ios::in | std::ios::binary);

This works because the Microsoft c++ implementation has an extension to allow fstream to be opened with a wide character string.

在将文件名传递给fstream之前,使用WideCharToMultiByte和CP_ACP转换UTF-16字符串。

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