Folder traversal code C ++ (Win32 platform)

Source: Internet
Author: User
Recently, I continued to learn C ++ and wrote code for Traversing folders. I hope you can advise me.
  1. Void common: file: readfiles (const wstring & directory, const wstring & Extentions, void (* filemethod) (const wstring &))
  2. {
  3. Win32_find_data finddata;
  4. Zeromemory (& finddata, sizeof (finddata); // initialize finddata
  5. Wstring findfilename (directory );
  6. Wstring fulldirectoryname (directory );
  7. If (directory [directory. Length ()-1] = '//')
  8. {
  9. Findfilename + = Extentions;
  10. Fulldirectoryname = directory;
  11. }
  12. Else
  13. {
  14. Findfilename + = l' // '+ Extentions;
  15. Fulldirectoryname + = l '//';
  16. }
  17. Handle hfind = findfirstfile (findfilename. c_str (), & finddata );

If (hfind = invalid_handle_value)
Return;

Findnextfile (hfind, & finddata );

Vector <wstring> dirlist;
While (findnextfile (hfind, & finddata ))
{
If (finddata. dwfileattributes & file_attribute_directory) = file_attribute_directory)
{
Dirlist. push_back (fulldirectoryname + finddata. cfilename );
}
Else

{
Wstring fullfilename (fulldirectoryname + finddata. cfilename );
Filemethod (fullfilename); // What do you do to traverse a file?
}
}

  1. For (vector <wstring >:: iterator iter = dirlist. Begin ();
  2. ITER! = Dirlist. End (); ++ ITER)
  3. {
  4. Readfiles (* ITER, Extentions, filemethod );
  5. }
  6. }

In this Code, I used a function pointer to process what to do after traversing a file. The declaration method of C ++ function pointers is really great. If you can support writing to lambda expressions, it is perfect.

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.