You wouldn't imagine something as basic as opening a file using the C++ standard library for a Windows application was tricky ... but it appears to be. By Unicode here I mean UTF-8, but I can convert to UTF-16 or whatever, the point is getting an ofstream instance from a Unicode filename. Before I hack up my own solution, is there a preferred route here ? Especially a cross-platform one ?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- 如何让cmd.exe 执行 UNICODE 文本格式的批处理?
- 怎么把Windows开机按钮通过修改注册表指向我自己的程序
- Warning : HTML 1300 Navigation occured?
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
If you're using Qt mixed with
std::ifstream
:Use
std::wofstream
,std::wifstream
andstd::wfstream
. They accept unicode filename. File name has to bewstring
, array ofwchar_t
s, or it has to have_T()
macro, or prefixL
before the text.The current versions of Visual C++ the std::basic_fstream have an
open()
method that take a wchar_t* according to http://msdn.microsoft.com/en-us/library/4dx08bh4.aspx.Have a look at Boost.Nowide:
The C++ standard library is not Unicode-aware.
char
andwchar_t
are not required to be Unicode encodings.On Windows,
wchar_t
is UTF-16, but there's no direct support for UTF-8 filenames in the standard library (thechar
datatype is not Unicode on Windows)With MSVC (and thus the Microsoft STL), a constructor for filestreams is provided which takes a
const wchar_t*
filename, allowing you to create the stream as:However, this overload is not specified by the C++11 standard (it only guarantees the presence of the
char
based version). It is also not present on alternative STL implementations like GCC's libstdc++ for MinGW(-w64), as of version g++ 4.8.x.Note that just like
char
on Windows is not UTF8, on other OS'eswchar_t
may not be UTF16. So overall, this isn't likely to be portable. Opening a stream given awchar_t
filename isn't defined according to the standard, and specifying the filename inchar
s may be difficult because the encoding used by char varies between OS'es.