Tips:
- The FileSystem library provides two header files, one is <boost/filesystem.hpp>, and the header file contains the main library contents. It provides important operations on the file system. At the same time it defines a class path, as you can imagine, this is a portable path representation, which is the foundation of the FileSystem library.
- One is <boost/filesystem/fstream.hpp>, a supplement to Std::fstream, which uses the class boost::p ath as a parameter, which makes the filesystem library more intimate with the standard library.
- Because file systems are shared across most systems, different processes can operate on the same object at the same time, so Filesysetm does not provide this feature guarantee. Of course, such assurances are not possible, or at least expensive.
- FileSystem at any time, as long as the corresponding task can not be completed, it can throw a Basic_filesystem_error exception, of course, not always throw an exception, because the library can turn off this feature when compiling. There are also two functions that provide no exception versions, because they are not exceptions when the task cannot be completed.
- All contents of the FileSystem library are defined in a sub-namespace of the boost namespace, which is called Boost::filesytem. After using Boost.filesytem, the "-lboost_filesystem-mt" option is required when linking, as this requires additional links and is not a repository for pure header files.
- The boost library used in this article is 1_54
#include <boost/filesystem.hpp> {boost::filesystem::p ath Path ("/test/test1"); Initialize Boost::filesystem::p ath Old_cpath=boost::filesystem::current_path (); Get current directory Boost::filesystem::p ath File_path = old_cpath/"file"; Path supports overloading/operator if (boost::filesystem::exists (File_path))//Judging file existence {std::string strpath = file_path.string (); int x = 1;} else {//directory does not exist; Boost::filesystem::create_directory (File_path); Directory does not exist, create} bool bisdirectory = Boost::filesystem::is_directory (File_path); Determine if File_path is a directory boost::filesystem::recursive_directory_iterator beg_iter (file_path); Boost::filesystem:: Recursive_directory_iterator end_iter;for (; Beg_iter! = End_iter; ++beg_iter) {if (boost::filesystem::is_directory (* Beg_iter)) {continue;} Else{std::string strpath = Beg_iter->path (). String (); Traverse out the file name int x=1;}} Boost::filesystem::p ath New_file_path = file_path/"Test.txt"; if (Boost::filesystem::is_regular_file (New_file_path) )//Determine if the normal file {UINT Sizefile = boost::filesystem::file_size (new_file_pATH); File size (bytes) int x = 1;} Boost::filesystem::remove (New_file_path);//delete file New_file_path}
Boost::filesystem Common usage Explanation