I want to read / write a file with a unicode file name using boost filesystem, boost locale on Windows (mingw) (should be platform independent at the end).
This is my code:
#include <boost/locale.hpp>
#define BOOST_NO_CXX11_SCOPED_ENUMS
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
namespace fs = boost::filesystem;
#include <string>
#include <iostream>
int main() {
std::locale::global(boost::locale::generator().generate(""));
fs::path::imbue(std::locale());
fs::path file("äöü.txt");
if (!fs::exists(file)) {
std::cout << "File does not exist" << std::endl;
}
fs::ofstream(file, std::ios_base::app) << "Test" << std::endl;
}
The fs::exists
really checks for a file with the name äöü.txt
.
But the written file has the name äöü.txt
.
Reading gives the same problem. Using fs::wofstream
doesn't help either, since this just handles wide input.
How can I fix this using C++11 and boost?
Edit: Bug report posted: https://svn.boost.org/trac/boost/ticket/9968
To clarify for the bounty: It is quite simple with Qt, but I would like a cross platform solution using just C++11 and Boost, no Qt and no ICU.
äöü.txt
, it looks like the literal is already UTF8, exceptboost::fs::path
is treating it as if it were CodePage 1252. Or more likely,boost::fs::path
is ignoring the encoding altogeather, and simply passing to the OS, and the OS is assuming it's codepage 1252. – Flatfishfs::exists
is working, so that means that the error must be inboost::fs::ofstream
. I would guess it's detecting that you're compiling with GCC and so incorrectly deciding to pass the OS a UTF8 encoded filename. That would be a boost bug. (An answer was deleted, but OP clarified problem is identical for wide string literal) – Flatfishäöü
are not in the source character set; try replacing them with the equivalent hex literals (I'm assuming you mean the versions of these characters that are storable in an 8-bit char). – Sottedfs::exists
work? It really seems to be a problem in the filesystem streams, so I#m looking for a solution without them, or a fix for them. – Conveyancingint main() { std::cout << "äöü"; return 0; }
what would output be ? (includes omitted for clarity ...) – Grecism