platform way to write Unicode to a file

I use Boost 1.60.0 on Windows, but I'm writing a cross-platform program. I'm trying to save filenames into a file and read them out later. The file may be text or binary and its encoding does not matter. I get the filenames by iterating through a directory using boost::filesystem::directory_iterator.

The filenames may contain Unicode characters and I can't find a cross-platform way to write or read them.

On Linux I can do it with ofstream and ifstream using boost::filesystem::path class's << and >> operators.

On Windows, however, I need to use wofstream and wifstream and imbue them with a UTF-8 locale, like so:

std::wofstream outFile("./outfile");
boost::locale::generator gen;
outFile.imbue(gen("UTF-8"));

And then use << and >> operators.

So, I currently have different code for Windows and for Linux, but I'd prefer a solution that doesn't require me to write different code for different systems.

链接地址: http://www.djcxy.com/p/9260.html

上一篇: 查找当前目录和文件的目录

下一篇: 平台的方式来编写Unicode文件