Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open an std::fstream (ofstream or ifstream) with a unicode filename?

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 ?

like image 232
Andrew Beatty Avatar asked May 04 '09 20:05

Andrew Beatty


People also ask

How do I get the fstream filename?

The fstream class doesn't store the filename, and doesn't provide any function for retrieving it. So one way to keep track of this information is to use std::map as: std::map<std::fstream*, std::string> stream_file_table; void f() { //when you open a file, do this: std::fstream file("somefile.

What is the difference between ofstream and fstream?

fstream inherits from iostream , which inherits from both istream and stream . Generally ofstream only supports output operations (i.e. textfile << "hello"), while fstream supports both output and input operations but depending on the flags given when opening the file.

What type is Ifstream?

ifstream is an input file stream. It is a special kind of an istream that reads in data from a data file. ofstream is an output file stream. It is a special kind of ostream that writes data out to a data file.


1 Answers

The C++ standard library is not Unicode-aware. char and wchar_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 (the char 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:

wchar_t const name[] = L"filename.txt"; std::fstream file(name); 

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'es wchar_t may not be UTF16. So overall, this isn't likely to be portable. Opening a stream given a wchar_t filename isn't defined according to the standard, and specifying the filename in chars may be difficult because the encoding used by char varies between OS'es.

like image 146
jalf Avatar answered Sep 19 '22 07:09

jalf