我正在使用 mingw 在 Windows 7 下工作。我遇到了 unicode 文件名的一些奇怪行为。我的程序需要是可移植的,并且我正在使用boost::filesystem (v 1.53)
它来处理文件路径。
这一切都进展顺利,直到我需要用 unicode 文件名打开文件。这与文件的内容无关,而是文件的名称。
我尝试了以下方法:为了测试C:\UnicodeTest\вячеслав
,我创建了一个名为的文件夹,并尝试在其中创建一个文件,方法是将文件名附加test.txt
到 boost wpath。由于某种原因,文件的创建失败。我正在使用 boost'sfstream
并且当我尝试打开文件时,设置了流的失败位。现在有趣的是,当我将文件夹名称附加到路径时,调用create_directories()
成功并创建了正确的目录C:\UnicodeTest\вячеслав\folder
。
我真的不明白为什么它不适用于文件。这是我使用的代码:
boost::filesystem::wpath path;
// find the folder to test
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;
}