The Boost C++ Libraries

File Streams

The standard defines various file streams in the header file fstream. These streams do not accept parameters of type boost::filesystem::path. If you want to open file streams with objects of type boost::filesystem::path, include the header file boost/filesystem/fstream.hpp.

Example 35.19. Using boost::filesystem::ofstream
#include <boost/filesystem/fstream.hpp>
#include <iostream>

using namespace boost::filesystem;

int main()
{
  path p{"test.txt"};
  ofstream ofs{p};
  ofs << "Hello, world!\n";
}

Example 35.19 opens a file with the help of the class boost::filesystem::ofstream. An object of type boost::filesystem::path can be passed to the constructor of boost::filesystem::ofstream. The member function open() also accepts a parameter of type boost::filesystem::path.