Home:ALL Converter>boost::filesystem and Unicode under Linux and Windows

boost::filesystem and Unicode under Linux and Windows

Ask Time:2011-03-15T10:58:49         Author:Roger Dahl

Json Formatter

The following program compiles in Visual Studio 2008 under Windows, both with Character Set "Use Unicode Character Set" and "Use Multi-Byte Character Set". However, it does not compile under Ubuntu 10.04.2 LTS 64-bit and GCC 4.4.3. I use Boost 1.46.1 under both environments.

#include <boost/filesystem/path.hpp>
#include <iostream>

int main() {
  boost::filesystem::path p(L"/test/test2");
  std::wcout << p.native() << std::endl;
  return 0;
}

The compile error under Linux is:

test.cpp:6: error: no match for ‘operator<<’ in ‘std::wcout << p.boost::filesystem3::path::native()’

It looks to me like boost::filesystem under Linux does not provide a wide character string in path::native(), despite boost::filesystem::path having been initialized with a wide string. Further, I'm guessing that this is because Linux defaults to UTF-8 and Windows to UTF-16.

So my first question is, how do I write a program that uses boost::filesystem and supports Unicode paths on both platforms?

Second question: When I run this program under Windows, it outputs:

/test/test2

My understanding is that the native() method should convert the path to the native format under Windows, which is using backslashes instead of forward slashes. Why is the string coming out in POSIX format?

Author:Roger Dahl,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/5307033/boostfilesystem-and-unicode-under-linux-and-windows
Philipp :

If you want to use the wide output streams, you have to convert to a wide string:\n\n#include <boost/filesystem/path.hpp>\n#include <iostream>\n\nint main() {\n boost::filesystem::path p(L\"/test/test2\");\n std::wcout << p.wstring() << std::endl;\n return 0;\n}\n\n\nNote that AFAIK using wcout doesn't give you Unicode output on Windows; you need to use wprintf instead.",
2011-03-15T14:37:51
filmor :

Your understanding of native is not completely correct:\n\nNative pathname format: An implementation defined format. [Note: For POSIX-like operating systems, the native format is the same as the generic format. For Windows, the native format is similar to the generic format, but the directory-separator characters can be either slashes or backslashes. --end note]\n\nfrom Reference\n\nThis is because Windows allows POSIX-style pathnames, so using native() won't cause problems with the above.\n\nBecause you might often get similar problems with your output I think the best way would be to use your preprocessor, i.e.:\n\n#ifdef WINDOWS\nstd::wostream& console = std::wcout;\n#elif POSIX\nstd::ostream& console = std::cout;\n#endif\n\n\nand something similar for the string-class.",
2011-03-15T08:59:04
yy