File traversal in Windows (using cfindfile)

Source: Internet
Author: User

I have always wanted to do this. I started preparing for the first two days, searched for the cfindfile information, and understood the ideas, so I started to do it directly.

A file system is a part of an operating system. Therefore, file operations depend on the interface functions provided by the operating system.

In this case, I directly used a class for file search in Windows to implement file traversal.

 

In fact, a file directory is a tree, and each directory has many subdirectories and files, just like a node.

When using cfindfile for traversal, you must first call the findfile function, and then call the findnextfile function to traverse the current directory.

To facilitate the use of the console project, I need to prompt a bit here:

Because windowsProgramUnicode encoding is used by default in the design. Therefore, wcout is required for output when you use the console program.

When wcout is used for output, if a Chinese character is encountered, the program gets stuck and stops running.

The solution is to add the following function call:

Setlocale (lc_all, "CHS ");

I found this function on the Internet. I encountered this problem because the Chinese characters were stuck when I traversed the C directory. I found that a file name only outputs the preceding numbers and English parts, chinese characters are not output.

 

First, I will show my achievements:

The last run is completed:

Is the effect okay? All directories should be listed.

Take a look at the function below:

1   Void  Traversfile (cstring cspath)  2   {  3 Cstring csprepath = Cspath;  4 Cstring csnextpath = Cspath;  5   Cfilefind ff;  6 Cspath + = _ T ( "  *.*  " );//  Traverse all directories at this level  7       Int Nresult = Ff. findfile (cspath );  8       While  (Nresult)  9   {  10 Nresult = Ff. findnextfilew ();  11           If (FF. isdirectory ()&&! Ff. isdots ()) 12   {  13 Wcout <(lpctstr) ff. getfilepath () < Endl;  14 Csnextpath + = Ff. getfilename ();  15 Csnextpath + = _ T ( "  \\  "  );  16   Traversfile (csnextpath );  17  }  18 Csnextpath = Csprepath;  19   }  20 }

The main method used here is recursive call. I think this method is relatively simple andCodeWe can see that this function has only 20 rows in total and the layers are clear.

I think what we need to talk about is the if judgment, because there are two points worth attention in that section.

The isdirectory function is used to determine whether the selected file is a file or a directory. The so-called Directory is actually a folder ~

If it is a folder, output the path, integrate the folder path, and send it to the recursive call function to traverse the files in the current folder.

At the beginning, the parameter of this function is a subdirectory. For example, if the f disk is traversed by me, the input parameter is f :\\

After this parameter is passed in, *. * will be added to the end of the file. At this time, the findfile function will start searching for all the files and folders in the current directory.

Basically, it is the depth-first traversal of a tree.

The main function code is very simple, but can complete the function well:

 1  Using   Namespace  STD;  2   /*  Changed to 20130625 for file search  */  3   Int _ Tmain ( Int Argc, tchar * argv [], tchar * Envp [])  4   {  5 Setlocale (lc_all, "  CHS "  );  6 Cstring cs = _ T ( "  F :\\  "  );  7   Traversfile (CS );  8 System ( "  Pause  "  );  9       Return   0 ;  10 }

 

Not bad?

 

Related Article

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.