Let's consider the following lines of code:
std::ofstream ofs("dir/subdir/file.txt"); ofs << "Boost.Filesystem is fun!";
In these lines, we attempt to write something to file.txt
in the dir/subdir
directory. This attempt will fail if there is no such directory. The ability to work with filesystems is necessary for writing a good working code.
In this recipe, we'll construct a directory and a subdirectory, write some data to a file, and try to create symlink
. If the symbolic link's creation fails, erase the created entities. We should also avoid using exceptions as a mechanism of error reporting, preferring some kind of return codes.
Let's see how that can be done in an elegant way using Boost.
Basic knowledge of C++ and the std::ofstream
class is required for this recipe.
The ;Boost.Filesystem
is not a header-only library, so code in this recipe requires linking against the boost_system
and boost_filesystem
libraries.