The boost. filesystem Library provides a portable tool for querying paths, files, and directories. It has been accepted by the C ++ Standards Committee for inclusion in tr2.
Compile
Compile the boost. filesystem library before using it. For more information, see boost compilation.
Header file
#include <boost/filesystem.hpp>
The content of all boost. filesystem libraries is in the namespace boost: filesystem.
Understanding the basic_path class
Basic_path is the most important class in the boost. filesystem library. It stores the path and file name in a system-independent manner. Like STD: basic_string, the path and wpath are specially modified for char and wchar_t.
Basic_path constructor:
basic_path( const string_type & s );
basic_path( const value_type * s );
template <class InputIterator> basic_path(InputIterator s, InputIterator last);
The input parameter is a string (or character iterator) that represents the path name. You can enter the native path name of the system or the portable path name.
Native pathnames are not easy to say, such as c: \ windows; D: \ ABC \ ttt.txt.
The portable path name is the same as the Unix path definition and uses "/" as the path separator.
Basic_path member functions:
Member Functions |
Function |
Template <class inputiterator> basic_path & append (inputiterator first, inputiterator last ); |
Append the path element in string s or Character Sequence [first, last) to the saved path. |
Basic_path & remove_filename (); |
Remove the file name from the path |
Basic_path & replace_extension (const string_type & new_extension = ""); |
Replace Extension |
String_type string () |
Get the portable path name |
String_type file_string () |
Obtain the system native file name |
String_type directory_string () |
Obtain the native path name of the system. |
String_type root_name () const; |
Get root name |
String_type root_directory () const; |
Get the root directory |
Basic_path root_path () const; |
Root path: Root name + root directory |
Basic_path relative_path () const; |
Get relative path |
String_type filename () const; |
Get File Name |
Basic_path parent_path () const; |
Parent path: root path + relative path |
String_type stem (const Path & P) const; |
Get the file name without the extension |
String_type extension (const Path & P) const; |
Get the extension |
Bool empty () const; |
PATH is not assigned a value. |
Bool is_complete () const; |
Whether the path is complete |
Bool has_root_path () const; Bool has_root_name () const; Bool has_root_directory () const; Bool has_relative_path () const; Bool has_filename () const; Bool has_branch_path () const; |
Indicates whether a specified item is included in a route. |
Test code:
- # Include "Boost/filesystem. HPP" // contains all the required boost. filesystem declarations
- # Include <iostream> // use STD: cout
- Namespace FS = boost: filesystem;
- // Macro fstest: test the member function of F and output the member function name and result.
- # Define fstest (x) STD: cout <# X # ":" <F. X <STD: Endl
- Int main ()
- {
- FS: path F ("\ folder1 \ folder2 \ folder3 \ filename. Ext ");
-
- Fstest (string ());
- Fstest (file_string ());
- Fstest (directory_string ());
- Fstest (root_name ());
- Fstest (root_directory ());
- Fstest (root_path ());
- Fstest (relative_path ());
- Fstest (filename ());
- Fstest (parent_path ());
- Fstest (STEM ());
- Fstest (extension ());
-
- Fstest (replace_extension ("new "));
- Char Buf [] = "hello ";
- Fstest (append (BUF, BUF + sizeof (BUF )));
- Fstest (remove_filename ());
-
- Return 0;
- }
Output:
string(): /folder1/folder2/folder3/filename.ext
file_string(): \folder1\folder2\folder3\filename.ext
directory_string(): \folder1\folder2\folder3\filename.ext
root_name():
root_directory(): /
root_path(): /
relative_path(): folder1/folder2/folder3/filename.ext
filename(): filename.ext
parent_path(): /folder1/folder2/folder3
stem(): filename
extension(): .ext
replace_extension("new"): /folder1/folder2/folder3/filename.new
append(buf, buf+sizeof(buf)): /folder1/folder2/folder3/filename.new/hello
remove_filename(): /folder1/folder2/folder3/filename.new/
Common functions
Function Name |
Function |
System_complete (PATH ); |
Returns the complete path (relative path + current path) |
Exists (PATH ); |
Whether the file exists |
Is_directory (PATH ); Is_directory (file_status ); |
Whether it is a path |
Is_regular_file (PATH ); Is_regular_file (file_status ); |
Whether it is a common file |
Is_symlink (PATH ); Is_symlink (file_status ); |
Is it a linked file? |
File_status status (PATH ); |
Returns the status corresponding to the path name. |
Template <class path> const Path & initial_path (); |
Obtain the current path of the system when the program is running. |
Template <class path> path current_path (); |
Obtain the current system path. |
Template <class path> void current_path (const Path & P ); |
Change current path |
Template <class path> space_info space (const Path & P ); |
Obtain the space information in the specified path. space_info has three member variables: capacity, free, and available, indicating capacity, available space, and available space respectively. |
Template <class path> STD: time_t last_write_time (const Path & P ); |
Last modification time |
Template <class path> void last_write_time (const Path & P, const STD: time_t new_time ); |
Last modification time |
Template <class path> bool create_directory (const Path & DP ); |
Create path |
Template <class path1, class path2> void create_hard_link (const path1 & to_p, const path2 & from_p ); Template <class path1, class path2> error_code create_hard_link (const path1 & to_p, Const path2 & from_p, error_code & EC ); |
Create a hard link |
Template <class path1, class path2> void create_symlink (const path1 & to_p, const path2 & from_p ); Template <class path1, class path2> error_code create_symlink (const path1 & to_p, const path2 & from_p, error_code & EC ); |
Create soft link |
Template <class path> void remove (const Path & P, system: error_code & EC = singular ); |
Delete an object |
Template <class path> unsigned long remove_all (const Path & P ); |
Recursively delete all content in P and return the number of deleted files |
Template <class path1, class path2> void Rename (const path1 & from_p, const path2 & to_p ); |
Rename |
Template <class path1, class path2> void copy_file (const path1 & from_fp, const path2 & to_fp ); |
Copy an object |
Template <class path> path complete (const Path & P, const Path & base = initial_path <path> ()); |
Base is used as the base, P is used as the relative path, and the complete path is returned. |
Template <class path> bool create_directories (const Path & P ); |
Create path |
Path iterator basic_directory_iterator
Constructor:
explicit basic_directory_iterator(const Path& dp);
basic_directory_iterator();
basic_directory_iterator
Obtain the directory from the constructor parameters.operator++
It finds and obtains the next file name until the end of the Directory element. Constructors without Parametersbasic_directory_iterator()
Always construct an end iterator object, which is the only valid iterator used for the end condition.
Sample Code to get all the file names in the specified directory:
- Void find_file (const FS: Path & dir_path)
- {
- If (! FS: exists (dir_path) return;
- FS: directory_iterator end_itr; // an end iterator is generated by default.
- For (FS: directory_iterator itr (dir_path );
- Itr! = End_itr;
- ++ Itr)
- {
- If (FS: is_directory (itr-> Status ()))
- {
- Find_file (itr-> path (); // recursive search
- }
- Else
- {
- STD: cout <* itr <STD: Endl;
- }
- }
- }
Basic_recursive_directory_iterator
The iterator that recursively traverses the directory. Its construction parameters are the same as basic_directory_iterator.operator++
If the current value is a directory, go to the next level directory.
It has three member functions:
Function Name |
Function |
Int level () const; |
Returns the current search depth. |
Void POP (); |
After Pop () is called, the next increment will directly return to the upper-level directory |
Void no_push (); |
After no_push () is called, even if the next element is of the Directory type |
Sample Code to obtain all the file names in the specified directory (same as the preceding example ):
- Void find_file2 (const FS: Path & dir_path)
- {
- FS: recursive_directory_iterator end_itr; // an end iterator is generated by default.
- For (FS: recursive_directory_iterator itr (dir_path );
- Itr! = End_itr;
- ++ Itr)
- {
- STD: cout <itr. Level () <* itr <STD: Endl;
- }
- }
Boost learning-portable path operation-filesystem