(ix) Boost library file processing filesystem
FileSystem Library is a portable file system operations Library, it has done a lot of work at the bottom, using the POSIX standard to represent the path of the file system, so that C + + has a similar scripting language features, you can work across the platform directory, file, write Generic Script program.
The 1.path constructor can accept a C string and a string, or it can be a sequence interval that specifies the first-end iterator.
2.filesystem provides a series of file name (or directory) check functions.
3. There are rich functions for obtaining filenames, directory names, determining file attributes, and so on.
The 4.filesystem library uses exceptions to handle errors that occur during file operations.
The 5.filesystem library provides a file status class File_status and a set of related functions that examine the various properties of a file, such as whether it exists, whether it is a directory, whether it is a symbolic link, and so on.
6.filesystem provides a small amount of file attribute operations, such as read-only, archive, etc. under Windows, and read and write permissions under Linux.
7. File operations, such as creating directories, renaming files, deleting files, copying files, and so on.
8.basic_directory_iterator provides the ability to iterate through all the files in a directory.
First, the basic usage of the path class
Note the difference between/= and + =,/= means append subordinate directory, + =
Dir("C:\\Windows");
"System32"; //Append subordinate directory
"services.exe";
std::Std::endl;
std::cout << dir.Std::endl; //Convert to std::string type
std::Std::endl; //drive letter name: C:
std::Std::endl; //root directory: "\"
std::Std::endl; //root path: "\ C"
std::Std::endl; //relative path: Windows\system32\services.exe
std::Std::endl; //Parent directory: C:\Windows\System32
std::Std::endl; //File name: Services.exe
std::Std::endl; //File name without extension: Services
std::Std::endl; //extension:. exe
Second, common functions and exception handling
Function name action
System_complete (path); Returns the full path (relative path + current path)
exists (path); Whether the directory 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 normal file
Is_symlink (path);
Is_symlink (File_status); is a linked file
File_status status (Path); Returns the status of the path name
Initial_path (); Gets the current path of the system while the program is running
Current_path (); Get the current path of the system
Current_path (const path& p); Change the current path
Space_info Space (const path& p); Given the spatial information under the specified path, Space_info has a capacity, free, and available three member variables representing capacity, remaining space, and available space, respectively.
Last_write_time (const path& p); Last Modified Time
Last_write_time (const path& p, const std::time_t new_time); Modify the last modified time
BOOL Create_directory (const path& DP); Build path
Create_hard_link (const path1& to_p, const path2& from_p);
Error_code create_hard_link (const path1& to_p, const path2& from_p, error_code& EC); Create a hard link
Create_symlink (const path1& to_p, const path2& from_p);
Create_symlink (const path1& to_p, const path2& from_p, error_code& EC); Create a soft link
Remove (const path& P, system::error_code & ec = singular); deleting files
Remove_all (const path& p); Recursively deletes all content in P, returning the number of deleted files
Rename (const path1& from_p, const path2& to_p); Renaming
Copy_file (const path1& FROM_FP, const path2& TO_FP); Copy files
Omplete (const path& p, const path& base=initial_path<path> ()); Takes base base, p as relative path, returns its full path
Create_directories (const Path & P); Build path
Common functions and exceptions
Try
{
Dir2("C:\\Windows\\System32");
ASSERT (Is_directory (DIR2)); //Determine if a directory
ASSERT (Exists (DIR2)); //Determine if the directory exists
}
catch (filesystem_error& e)
{
std::Std::endl;
std::Std::endl;
}
Iii. Directory iterations
The Boost library provides two iterators
directory_iterator :只支持本层目录遍历
Recursive_directory_iterator: Supports deep traversal
Output files in the specified directory:
Dir2("C:\\Windows\\System32");
Directory_iterator end;
For (pos(DIR2); pos! = end; pos++)
{
std::Std::endl;
}
Using deep traversal
typedef recursive_directory_iterator Rd_iterator;
Path Dir2 ("e:\\student");
Rd_iterator end;
pos++)
{
If the depth is greater than 4 levels, no further further
if (is_directory (*4)
{
Pos.no_push ();
}
If the directory has a nofind.txt file, jump out of the directory
if (*"nofind.txt")
{
Pos.pop ();
}
}
Iv. examples
Locate the matching files in the specified directory and sub-directories
#include <iostream>
#include <boost/filesystem.hpp>
#include <boost/xpressive/xpressive_dynamic.hpp>
#include <boost/algorithm/string.hpp>
#include <vector>
namespace Boost::filesystem;
namespace Boost::xpressive;
typedef recursive_directory_iterator Rd_iterator;
Get Filtered Files
voidgetfilterfile(conststd::string& Filter,std::vector<path> & Vecfile)
{
Path FilterPath = dir/filter;
Path FilterDir1 = Filterpath.parent_path ();
std::string filename = Filterpath.filename (). String ();
Convert file wildcard character to regular expression
std::"." \\. "*".*");
Sregex reg = Sregex::compile (str);
if (!exists (filterDir1) | |!is_directory (FILTERDIR1))
{
Return
}
Finds all files in the current directory and subdirectories and adds them to the list if a regular expression is met
Rd_iterator end;
For (pos(FILTERDIR1); pos! = end; pos++)
{
if (!is_directory (*pos) && regex_match (Pos->path (). FileName (). String (), Reg))
{
Vecfile.push_back (Pos->path ());
}
}
}
(ix) Boost library file processing filesystem