Boost learning-portable path operation-filesystem

Source: Internet
Author: User

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:
  1. # Include "Boost/filesystem. HPP" // contains all the required boost. filesystem declarations
  2. # Include <iostream> // use STD: cout
  3. Namespace FS = boost: filesystem;
  4. // Macro fstest: test the member function of F and output the member function name and result.
  5. # Define fstest (x) STD: cout <# X # ":" <F. X <STD: Endl
  6. Int main ()
  7. {
  8. FS: path F ("\ folder1 \ folder2 \ folder3 \ filename. Ext ");
  9.  
  10. Fstest (string ());
  11. Fstest (file_string ());
  12. Fstest (directory_string ());
  13. Fstest (root_name ());
  14. Fstest (root_directory ());
  15. Fstest (root_path ());
  16. Fstest (relative_path ());
  17. Fstest (filename ());
  18. Fstest (parent_path ());
  19. Fstest (STEM ());
  20. Fstest (extension ());
  21.  
  22. Fstest (replace_extension ("new "));
  23. Char Buf [] = "hello ";
  24. Fstest (append (BUF, BUF + sizeof (BUF )));
  25. Fstest (remove_filename ());
  26.  
  27. Return 0;
  28. }

 

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_iteratorObtain 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:

  1. Void find_file (const FS: Path & dir_path)
  2. {
  3. If (! FS: exists (dir_path) return;
  4. FS: directory_iterator end_itr; // an end iterator is generated by default.
  5. For (FS: directory_iterator itr (dir_path );
  6. Itr! = End_itr;
  7. ++ Itr)
  8. {
  9. If (FS: is_directory (itr-> Status ()))
  10. {
  11. Find_file (itr-> path (); // recursive search
  12. }
  13. Else
  14. {
  15. STD: cout <* itr <STD: Endl;
  16. }
  17. }
  18. }

 

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 ):

  1. Void find_file2 (const FS: Path & dir_path)
  2. {
  3. FS: recursive_directory_iterator end_itr; // an end iterator is generated by default.
  4. For (FS: recursive_directory_iterator itr (dir_path );
  5. Itr! = End_itr;
  6. ++ Itr)
  7. {
  8. STD: cout <itr. Level () <* itr <STD: Endl;
  9. }
  10. }

Boost learning-portable path operation-filesystem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.