background preloader

Boost.Filesystem

Facebook Twitter

Filesystem Library. Introduction The Boost Filesystem Library provides portable facilities to query and manipulate paths, files, and directories.

Filesystem Library

The motivation for the library is the need to be able to perform portable script-like operations from within C++ programs. The intent is not to compete with Python, Perl, or shell languages, but rather to provide portable filesystem operations when C++ is already the language of choice. The design encourages, but does not require, safe and portable filesystem usage. The Filesystem Library supplies several headers, all in directory boost/filesystem: Header path.hpp provides class path, a portable mechanism for representing paths in C++ programs.

The organizing principle is that purely lexical operations on paths are supplied as class path member functions in path.hpp, while operations performed by the operating system on the actual external filesystem directories and files are provided in operations.hpp, primarily as free functions. Two-minute tutorial Examples. Recipe 10.11. Removing a Directory. Recipe 10.11. Removing a Directory Problem You need to remove a directory, and you want to do it portably, i.e., without using OS-specific APIs. Solution On most platforms, you will be able to use the rmdir system call that is shipped with most compilers as part of the C headers. Discover the Boost Filesystem Library. One of the most common issues with the C++ language—and indeed, the C++ standard—is the lack of a well-defined library that helps deal with file system queries and manipulation.

Discover the Boost Filesystem Library

This absence leads programmers to use the native operating system-provided application program interfaces (APIs), which makes for code that isn't portable across platforms. Consider a simple situation: You need to figure out whether a file is of type Directory. In the Microsoft® Windows® platform, you could do this by calling the GetAttributes library function, defined in the windows.h header file: DWORD GetFileAttributes (LPCTSTR lpFileName); For directories, the result should be FILE_ATTRIBUTE_DIRECTORY, and your code must check for the same. For programs with heavy-duty I/O, such inconsistencies imply a significant engineering effort for porting the code across platforms. Your first program using boost::filesystem. C++ - Convert Boost Type "Path" To string.

Problem Linking Boost Filesystem Library in Microsoft Visual C++ Filesystem relative path and current directory. The Boost C++ Libraries - Filesystem. A new edition of this book is available!

The Boost C++ Libraries - Filesystem

It has been published as a print book and can be bought from Barnes and Noble, Amazon and other bookstores. The new edition is up-to-date and based on the Boost C++ Libraries 1.47.0 (released in July 2011). Runtime parameters reference - Boost 1.46.1. Directories in C++ Directories in C++ - 10.11 Removing a Directory(Page 2 of 4 ) Problem You need to remove a directory, and you want to do it portably, i.e., without using OS-specific APIs. Solution On most platforms, you will be able to use the rmdir system call that is shipped with most compilers as part of the C headers. There is no standard C++, portable way to remove a directory. rmdir takes on different forms in different OSs, but regardless, you can use it to remove a directory. Example 10-17. #include <iostream>#include <direct.h> using namespace std; int main(int argc, char** argv) { Discussion The signature of rmdir is the same on most OSs, but the header file where it is declared is not.

If the target directory is not empty rmdir will return an error. If you want portability, and don't want to write a bunch of #ifdefs around the various OS-specific directory functions, you should consider using the Boost Filesystem library. Example 10-18. #include <iostream void removeRecurse(const path& p) { See Also. Filesystem Tutorial. Introduction Preliminaries Reporting the size of a file - (tut1.cpp) Using status queries to determine file existence and type - (tut2.cpp) Directory iteration plus catching exceptions - (tut3.cpp) Using path decomposition, plus sorting results - (tut4.cpp) Class path: Constructors, including Unicode - (tut5.cpp) Class path: Generic format vs.

Filesystem Tutorial

Native format Class path: Iterators, observers, composition, decomposition, and query - (path_info.cpp) Error reporting Introduction This tutorial develops a little command line program to list information about files and directories - essentially a much simplified version of the POSIX ls or Windows dir commands. We'll start with the simplest possible version and progress to more complex functionality. Along the way we'll digress to cover topics you'll need to know about to understand Boost.Filesystem. Source code for each of the tutorial programs is available, and you are encouraged to compile, test, and experiment with it. Preliminaries Try this: