boost::filesystem and Unicode under Linux and Windows

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:


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?

-------------Problems Reply------------

Your understanding of native is not completely correct:

Native 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]

from Reference

This is because Windows allows POSIX-style pathnames, so using native() won't cause problems with the above.

Because you might often get similar problems with your output I think the best way would be to use your preprocessor, i.e.:

#ifdef WINDOWS
std::wostream& console = std::wcout;
#elif POSIX
std::ostream& console = std::cout;

and something similar for the string-class.

If you want to use the wide output streams, you have to convert to a wide string:

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

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

Note that AFAIK using wcout doesn't give you Unicode output on Windows; you need to use wprintf instead.

Try this:

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

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

Category:windows Views:1 Time:2011-03-15

Related post

  • How can I copy a directory using Boost Filesystem 2011-12-21

    How can I copy a directory using Boost Filesystem? I have tried boost::filesystem::copy_directory() but that only creates the target directory and does not copy the contents. --------------Solutions------------- bool copyDir( boost::filesystem::path

  • Move C++ app with Boost from Linux to Windows with Visual Studio 6 2010-02-16

    I made a small program with Boost in Linux 2 yrs ago. Now I want to make it work in Windows. I found there are few .a files in my libs folder. I am wondering how to make it works in Windows? do I need to build Boost in Windows to get library or I can

  • Boost - cross compile - "from Linux" "to Windows" 2009-09-09

    I have downloaded "boost" (1.40.0) source code from their homepage "". I have Linux (Ubuntu 9.04 Jaunty) installed and trying to compile the boost libraries to the "WINDOWS" version (e.g. ".dll", NOT ".so") from my "LINUX" machine. And n

  • boost::filesystem - how to create a boost path from a windows path string on posix plattforms? 2010-06-02

    I'm reading path names from a database which are stored as relative paths in Windows format, and try to create a boost::filesystem::path from them on a Unix system. What happens is that the constructor call interprets the whole string as the filename

  • Wny Linux sends unicode keyboard events while Windows sends ANSI 2010-09-27

    Unde linux, the foreign keys such as ü Ü do not seem to work with this code: String.fromCharCode(e.charCode).toLowerCase(); The output is wrong under Linux, but Windows is Ok. Here is sample page to debug this: the output of foreign keys such as ü wi

  • Migrating file operations from Linux to Windows 2009-02-26

    I am using C++ ofstream to write out a file. I want to set the permissions to be only accessible by the user: 700. In unix; I suppose I can just issue a system("chmod 700 file.txt"); but I need this code to work on windows as well. I can use some win

  • Simultaneous C++ development on Linux and Windows 2009-08-04

    We have a handful of developers working on a non-commercial (read: just for fun) cross-platform C++ project. We've already identified all the cross-platform libraries we'll need. However, some of our developers prefer to use Microsoft Visual C++ 2008

  • Opening fstream with file with Unicode file name under Windows using non-MSVC compiler 2010-02-23

    I need to open a file as std::fstream (or actually any other std::ostream) when file name is "Unicode" file name. Under MSVC I have non-standard extension std::fstream::open(wchar_t const *,...)? What can I do with other compilers like GCC (most impo

  • Compose path (with boost::filesystem) 2010-05-04

    I have a file that describes input data, which is split into several other files. In my descriptor file, I first give the path A that tells where all the other files are found. The originator may set either a relative (to location of the descriptor f

  • Why is there no boost::filesystem::move_file? 2010-06-30

    I'm using boost filesystem to replace windows C++ functions like CopyFile and MoveFile get some kind of portability between windows and linux. I'm using copy_file but I have not been able to find anything that moves files like a 'move_file' function.

  • boost::filesystem::rename: Cannot create a file when that file already exists 2010-07-01

    I'm renaming a file using boost::filesystem, and sometimes the target file will exist. According to the boost documentation here: template void rename(const

  • Determine if Linux or Windows in C++ 2010-07-09

    I am writing a cross-platform compatible function in C++ that creates directories based on input filenames. I need to know if the machine is Linux or windows and use the appropriate forward or back slash. For the following code below, if the machine

  • MAX_PATH limitation in Boost.Filesystem 2011-01-05

    I want to use Boost.Filesystem library to manipulate paths, files and directories. My question is are paths longer than MAX_PATH supported? I know that in Win32API we have workaround "\\?\" but it's not supported by basic functions like PathAppend an

  • Boost::FileSystem Linking Problem 2011-08-29

    I downloaded and built the boost libraries (version 1.47.0) on Windows 7 (64bit) following the instructions here. Now when I want to use the Boost::Filesystem library I can include the header file without issue and it compiles my code file. The probl

  • Boost filesystem tutorial won't compile 2011-11-10

    I try to compile the boost filesystem tutorial in Code::Blocks And it comes up these errors Linking console executable: bin\Debug\Boost fs tut1.exe obj\Debug\main.o: In function `_static_initialization_and_destruction_0': D:/Documents/Coding/Libs/boo

  • Problem Linking Boost Filesystem Library in Microsoft Visual C++ 2009-01-22

    I am having trouble getting my project to link to the Boost (version 1.37.0) Filesystem lib file in Microsoft Visual C++ 2008 Express Edition. The Filesystem library is not a header-only library. I have been following the Getting Started on Windows g

  • How do I write a C++ program that will easily compile in Linux and Windows? 2009-01-25

    I am making a C++ program. One of my biggest annoyances with C++ is its supposed platform independence. You all probably know that it is pretty much impossible to compile a Linux C++ program in Windows and a Windows one to Linux without a deluge of c

  • how to perform boost::filesystem copy_file with overwrite 2009-05-06

    The Windows API function CopyFile has an argument BOOL bFailIfExists that allows you to control whether or not you want to overwrite the target file if it exists. The boost::filesystem copy_file function has no such argument, and will fail if the tar

  • linux to windows C++ byte array 2009-06-09

    I have to replicate the following Java functionality in C++ to get data from Linux to Windows. Is Winsock2 the best way to go?. Also, any reference code to suggest? TIA, B import java.nio.ByteBuffer; public class MessageXdr { private ByteBuffer buffe

Copyright (C), All Rights Reserved.

processed in 0.149 (s). 11 q(s)