views:

225

answers:

1

When using Boost Filesystem's createdirectory (and createdirectories) function in the following example, "/" is being replaced with "\".

boost::filesystem::path path ("/data/configSet");
boost::filesystem::create_directory(path);

This code snipped produces a directory called "data\configSet", instead of creating a subdirectory of "configSet" inside "data". The same problem occurs using createdirectories();

This issue does not occur when the code is executed on a Windows system. I am currently testing on Linux using Ubuntu 9.10

+1  A: 

It looks like for some reason boost::filesystem thinks that you are on Windows, not Linux, and thus is using Windows style pathnames (separated by \). Can you post a bit more information about how you are building Boost and how you're including the headers? Are you perhaps building a Windows version of Boost on Linux?

edit: I have tried setting myself up in a configuration as close to yours as possible. Ubuntu 9.10, libboost1.40-all-dev installed. When I compile and run the following program, it works as expected, creating a directory named configSet in /data.

#include <boost/filesystem.hpp>

int main() {
  boost::filesystem::path p("/data/configSet");
  boost::filesystem::create_directory(p);

  return 0;
}

Can you try compiling and running that program, with the following commands, and see if it gives you different results?

$ g++ -o boost-filesystem -lboost_filesystem boost-filesystem.cpp
$ ./boost-filesystem
Brian Campbell
As this is on Ubuntu, I just installed libboost1.40-all-dev using Synaptic.My development environment is Code::Blocks, so I've just included the boost-filesystem header as per usual: #include <boost/filesystem.hpp>and then set my linker options to -lboost_filesystem.
Dave
Brian, after trying your example and finding it worked (after removing the "/" from the beginning of "/data/configSet"), I dug a little deeper and discovered that Code::Blocks had set "WIN32" as an extra option in my compiler options. This was probably due to me importing the project from Visual Studio.Removing the WIN32 definition has resolved the issue. As you originally suggested this was the problem, I've marked your answer as the correct one.Thanks!
Dave