Boost:Filesystem
lastmod 2015-09-12created 2015-04-035 edits
Macro
-
BOOST_FILESYSTEM_DYN_LINK: Macro for dynamic linking. In this case, you must link theboost_filesystemlibrary. You must also link theboost_systemlibrary. -
BOOST_FILESYSTEM_NO_LIB: Macro for static linking.
Generic vs Native
Class path deals with two different pathname formats - generic format and native format. For POSIX-like file systems, these formats are the same. But for users of Windows and other non-POSIX file systems, the distinction is important. Even programmers writing for POSIX-like systems need to understand the distinction if they want their code to be portable to non-POSIX systems.
- The generic format is the familiar
/my_directory/my_file.txtformat used by POSIX-like operating systems such as the Unix variants, Linux, and Mac OS X. Windows also recognizes the generic format, and it is the basis for the familiar Internet URL format. The directory separator character is always one or more slash characters.
- The native format is the format as defined by the particular operating system. For Windows, either the slash or the backslash can be used as the directory separator character, so
/my_directory\my_file.txtwould work fine. Of course, if you write that in a C++ string literal, it becomes"/my_directory\\my_file.txt".
Current Directory
- Stackoverflow: Boost::file_system how to find out in which directory your executable is?
- Stackoverflow: Get path of executable
The current directory can be checked as follows.
// current working directory
fs::path full_path( fs::current_path<fs::path>() );
std::cout << full_path << std::endl;
std::cout << full_path.stem() << std::endl;
Temp file
A temporary file name can be obtained as follows.
// Boost.Filesystem VERSION 3 required
#include <string>
#include <boost/filesystem.hpp>
boost::filesystem::path temp = boost::filesystem::unique_path();
const std::string tempstr = temp.native(); // optional
Example
Check whether a file exists:
Delete a file:
Favorite site
- Boost Filesystem v3 Introduction
- Boost Filesystem v3 Reference Documentation
- Using filesystem in the boost library, Lesson 1
Extension filter
- How to get list of files with a specific extension in a given folder
- Can I use a mask to iterate files in a directory with Boost?
- Using boost range adaptors with a directory iterator range
Pages that link here