VC processing files, directories and corresponding directory strings

Source: Internet
Author: User

Introduced

This paper briefly introduces two ways of handling files and directories, one is Boost::filesystem library, the other is path functions in Windows shell on Win32 platform.

The interactive operation of file system and the processing of file and directory strings are common problems in programming activities. Here are two different ways in which you can greatly accelerate programming time and effort, and build applications that are more robust. The introduction of the relatively simple, in practical applications, we suggest that you need to see the appropriate help documents according to the specific needs.

The filesystem in the Boost library (www.boost.org) is a fairly useful library for file and directory operations, and can be cross-platform. The structure is clear and reasonable, the encapsulation is very rigorous and easy to use. For commonly used file traversal operations are done through iterators, and STL consistent. And this part of the class and interface is relatively small, help document is quite clear.

The following example is a way to find specific files by traversing directories and their subdirectories (excerpted from the boost official help document).

bool find_file( const path & dir_path, const std::string & file_name)
{
 if ( !exists( dir_path ) ) return false;
 directory_iterator end_itr; // default construction yields past-the-end
 for ( directory_iterator itr( dir_path ); itr != end_itr; ++itr )
 {
  if ( is_directory( *itr ) )
  {
   if ( find_file( *itr, file_name, path_found ) ) return true;
  }
  else if ( itr->leaf() == file_name )
  {
   return true;
  }
 }
 return false;
}

Description

1. Exists and is_directory are some of the methods that are located in the Boost::filesystem name space. FileSystem provides a path class and some methods for manipulating the class, including determining whether a directory exists, copying files, moving them, deleting them, and so on. The parameters of these methods all contain the Path object.

2. Directory_iterator is the iterator class used to traverse the directory, and the operation is simple. Note that the default constructed instance can be used as a flag for traversing the end, as shown in the code.

3. The leaf is a member method of the path class that returns the end of a directory string, possibly a directory name or a filename. The path class defines a number of similar member functions.

4. The entire method uses the depth first recursive algorithm to search.

Attention:

When you construct a path object with a directory string, filesystem defaults to the UNIX style, and if you are constructing a Windows-style catalog string on a Windows platform, note that the native flag is added, or an exception will occur. For example:

Path MyPath ("c:\\", native);

In fact, if you don't want to cross the platform, you can also choose to use a cluster of functions provided in the Windows Shell to handle the filename and path under WIN32. These functions are located in Shlwapi.dll and are stated in Shlwapi.h to note the link Shlwapi.lib file in the compilation options. Specific functions and usages can easily be found in MSDN, there are dozens of, very rich, basically you can think of the way to handle the directory and file strings can be found here. It is convenient to process files and directory strings, and URLs. The disadvantage is that it is not organized and structured like boost.

The location of this section in the VS2005 Help document is: Win32 and COM development/user interface/windows User experience/windows shell/windows Shell/shell Reference/shell lightweight Utility Functions/path functions should not be difficult to find in other versions of MSDN.

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.