The following knowledge points are transferred from: Click to open the original article link
Today, I picked up my hand and used C ++ to write a small tool, recursively traverse the file from the specified directory, and then perform some processing. I flipped through the boost filesystem library. In conclusion, I hope to get a better impression, so that I will not view the document again next time.
1. The path object is a cross-platform path object. There are many ways to access each part of the path, and also use its iterator to iterate each part of the path;
The path operator is very intuitive when constructing the directory structure.
For example, path P1;
Path P2 = p1/"something"; P1/= "XXX. XXX ";
2. there are some global functions in the filesystem namespace. For example, exists can determine whether a path exists, and is_directory function can determine whether a directory exists. file_size can be used to obtain the size. The size is of the exaggerated platform type, it can be 32-bit or 64-bit;
Other is methods include:
Is_empty
Is_other
Is_regular_file
Is_symlink
3. the most convenient function is to traverse all the content in the path. Directory_iterator.
Path P;
Directory_iterator (P) is the starting point of the iterator, and the non-parameter directory_iterator () is the end point of the iterator.
You can also perform recursive iteration to replace the above directory_iterator with recursive_directory_iterator.
4. Create a directory. Here we need to mention a method that is bool create_directories (const Path & P); If P is a directory (that is, is_diretory return true ). It recursively creates the entire directory structure, saving you the trouble of creating one directory one by one.
Other creation methods include:
Create_directories
Create_directory
Create_hard_link
Create_symlink
5. You can also copy directories.
Copy_directory
Copy_file
Copy_symlink
6. Delete remove recursively Delete remove_all
7. Rename
8. If <boost/filesystem/fstream. HPP> is included, fstream can accept path as a parameter.
The following code is used ):
# Include <iostream> # include <vector> # include <boost/filesystem. HPP> # include <boost/filesystem/operations. HPP> using namespace STD; namespace FS = boost: filesystem; void getallfilename (string pathname, vector <string> & savename) {FS: path fullpath (pathname ); // determine whether the directory exists if (! FS: exists (fullpath) {cout <"No thus path" <Endl; return;} // you can check whether the directory is if (! FS: is_directory (fullpath) {cout <"is not a direcotry" <Endl; return;} // use FS :: directory_iterator traverses the path content // end_iter defaults to the end of path FS: directory_iterator end_iter; For (FS: directory_iterator file_iter (fullpath); file_iter! = End_iter; ++ file_iter) {// judge whether the end is ". JSON "If (FS: Extension (* file_iter) = ". JSON ") {// convert the FS: path type to the string type savename. push_back (FS: system_complete (* file_iter ). leaf (). string () ;}}int main () {string path ("/usr/resources/scene"); vector <string> savename; // obtain all JSON file names in the specified directory and store them to getallfilename (path, savename); vector <string >:: iterator it = savename. begin (); For (; it! = Savename. End (); ++ it) {cout <* It <Endl;} return 0 ;}
Note:
When compiling the boost: filesystem library, an error is reported. Because boost_system is compiled first and then boost_filesystem is compiled, the following options can be added to Linker Options in the compiler project settings during compilation:
-Lboost_system-lboost_filesystem