Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does C++ traverse the files in the directory?

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how C++ traverses the files under the directory", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn how C++ traverses the files under the directory.

C++ traverses the files in the directory

Function: traverses all the files in the directory and returns the total number of files and subfolders (you can get all the file names by modifying them, etc.).

Example code:

# include "stdlib.h" # include "direct.h" # include "string.h" # include "io.h" # include "stdio.h" # include "iostream" using namespace std; class CBrowseDir {protected: / / the absolute path where the initial directory is stored, ending with'\ 'char mszInitDir [_ MAX_PATH]; public: / / default constructor CBrowseDir () / / set the initial directory to dir. If false is returned, it means that bool SetInitDir (const char * dir) is not available in the directory; / / start traversing files of the type specified by filespec in the initial directory and its subdirectories / / filespec can use the wildcard *? and cannot contain paths. / / if false is returned, the traversal process is aborted by the user bool BeginBrowse (const char * filespec); protected: / / traverses the files specified by filespec under the directory dir / / for subdirectories, iterative method / / if false is returned, the traversal file bool BrowseDir (const char * dir,const char * filespec) is aborted / / every time the function BrowseDir finds a file, it calls ProcessFile / / and passes the file name as an argument / / if false is returned, it means to abort traversing the file / / users can override the function and add their own processing code virtual bool ProcessFile (const char * filename) / / every time the function BrowseDir enters a directory, it calls ProcessDir / / and passes the name of the directory being processed and the name of the directory at the next level as parameters / / if you are processing the initial directory, parentdir=NULL / / users can overwrite the function and add their own processing code / / for example, the user can count the number of subdirectories here virtual void ProcessDir (const char * currentdir,const char * parentdir);} CBrowseDir::CBrowseDir () {/ / initializes m_szInitDir getcwd with the current directory; / / if the last letter of the directory is not'\', add a'\ 'int len=strlen (m_szInitDir) at the end; if (mszInitDir [len-1]! ='\\') strcat (m_szInitDir, "\\") } bool CBrowseDir::SetInitDir (const char * dir) {/ / first convert dir to the absolute path if (_ fullpath (massiszInitDirDirdagenology max path) = = NULL) return false; / / determine whether the directory exists if (_ chdir (m_szInitDir)! = 0) return false; / / if the last letter of the directory is not'\', add a'\ 'int len=strlen (m_szInitDir) at the end If (mszInitDir [len-1]! ='\\') strcat (m_szInitDir, "\"); return true;} bool CBrowseDir::BeginBrowse (const char * filespec) {ProcessDir (mleavszInitDirNull); return BrowseDir (mleavszInitDirNull);} bool CBrowseDir::BrowseDir (const char * dir,const char * filespec) {_ chdir (dir); / / first find the file long hFile in dir that meets the requirements _ finddata_t fileinfo; if ((hFile=_findfirst (filespec,&fileinfo))! =-1) {do {/ / check if it is not a directory / / if not, process if (! (fileinfo.attrib & _ A_SUBDIR)) {char filename [_ MAX_PATH]; strcpy (filename,dir); strcat (filename,fileinfo.name); cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report