Boost::filesystem Common usage Detailed __boost

Source: Internet
Author: User
Tags parent directory
Tip:

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, and 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, using class boost::p ath as a parameter to make the filesystem library more intimate with the standard library. Because file systems are shared by most systems, different processes can manipulate the same object at the same time, so Filesysetm does not provide a guarantee of this feature. Of course, this guarantee is also impossible, or at least expensive. FileSystem at any time, as long as cannot complete the corresponding task, it may throw the basic_filesystem_error exception, certainly does not always throw the exception, because can turn off this function when the library compiles. At the same time, there are two functions that provide an exception-free version, because it is not an exception when the task cannot be completed. All content of the filesystem library is defined in a subordinate namespace of the boost namespace, which is called Boost::filesytem. After using Boost.filesytem, the link needs to be added with the "-lboost_filesystem-mt" option because it requires additional links, not a library of plain header files. The boost library used in this paper is 1_54

#include <boost/filesystem.hpp> {boost::filesystem::p ath Path ("/test/test1"); Initialization of Boost::filesystem::p ath Old_cpath = Boost::filesystem::current_path (); Gets the directory of the current program Boost::filesystem::p ath Parent_path = Old_cpath.parent_path ();//Fetch Old_cpath's upper-level parent directory path 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 whether 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 ();
		Traversal out of the filename int x=1; }} Boost::filesysteM::p ath New_file_path = file_path/"Test.txt"; if (Boost::filesystem::is_regular_file (New_file_path))//To 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}



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.