Boost logo

Boost Users :

Subject: [Boost-users] [filesystem] Create file with a unicode filename/path
From: Konstantin Kronfeldner (konstantin.kronfeldner_at_[hidden])
Date: 2013-06-17 10:52:56


Hello,

I'm working under windows 7 with mingw. I have encountered some weird
behaviour with unicode filenames. My program needs to be portable, and
I'm using boost::filesystem (v 1.53) to handle the file paths.

This has all been going well, until I needed to open files with unicode
filenames. This is not about the content of the file, but the file's
name/path.

I tried the following: For testing I made a folder named
C:\UnicodeTest\|????????|and I tried creating a file inside of it, by
appending the file name test.txt to the boost wpath. For some reason the
creation of the file fails. I'm using boost's fstreams and when I try to
open the file, the failbit of the stream is set. Now the funny thing is,
that when I append a foldername to the path instead, a call to
create_directories() succeeds and creates the correct directory
C:\UnicodeTest\|????????|\folder.

I really don't understand why it won't work with a file.

I have also posted my problem on stackoverflow.
http://stackoverflow.com/questions/16875025/open-a-file-with-unicode-path
No luck so far.

This is the code I use:

boost::filesystem::wpath path;

// find the folder to test (There are no other folders in C:\UnicodeTest)
boost::filesystem::wpath dirPath = "C:\\UnicodeTest";
vector<boost::filesystem::wpath> files;
copy(boost::filesystem::directory_iterator(dirPath),
boost::filesystem::directory_iterator(), back_inserter(files));

for(boost::filesystem::wpath &file : files)
{
     if(boost::filesystem::is_directory(file))
     {
         path = file;
         break;
     }
}

// create a path for the folder
boost::filesystem::wpath folderPath = path / "folder";
// this works just fine
boost::filesystem::create_directories(folderPath);

// create a path for the file
boost::filesystem::wpath filePath = path / "test.txt";

boost::filesystem::ofstream stream;

// this fails
stream.open(filePath);

if(!stream)
{
     cout << "failed to open file " << path << endl;
}
else
{
     cout << "success" << endl;
}



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net