How to search through folders in deep C + +

Source: Internet
Author: User

Depth-First search traverses all files of a folder, because using Windows functions, you must use the C language;

Attention to the problem of character set, using "#undef UNICODE", shielding the problem caused by the character set;

Use vector<string> to store all file names because you want to use them recursively, so you need to set them to static and return shared_ptr pointers

The code is as follows:

/************************************************* File:main.cpp Copyright:c.l.wang author:caroline date:2013-11- Description: Depth-first recursive traversal of all files in a directory email:morndragon@126.com **************************************************/# undef UNICODE #include <iostream> #include <string> #include <vector> #include <memory > #include <cstring> #include <windows.h> std::shared_ptr<std::vector<std::string > > filelist (const std::string& folder_path) {static Std::shared_ptr<std::vector<std::string&gt ; > folder_files (new std::vector<std::string>);  
    Returns a pointer that requires iterative use of win32_find_data FindData;  
      
    HANDLE Herror;  
    int File_count (0); std::string File_path (Folder_path); path name std::string Full_file_path;  
    Full path name File_path.append ("/*.*");  
    Herror = FindFirstFile (File_path.c_str (), &finddata); if (Herror = = inValid_handle_value) {std::cout << "failed to search files." << Std::endl;  
    return nullptr; while (FindNextFile (Herror, &finddata)) {//Over ".", "..", "Q" if (0 = = strcmp (FindData . cFileName, ".")   
            ||   
            0 = strcmp (finddata.cfilename, "...") | |  
        0 = = strcmp (finddata.cfilename, "Q")) {continue;  
        }//full path Full_file_path.append (Folder_path);  
        Full_file_path.append ("/");  
        Full_file_path.append (Finddata.cfilename);  
      
        ++file_count; if (Finddata.dwfileattributes & file_attribute_directory) {//std::cout << file_count << ""  
            << full_file_path << "<Dir>" << Std::endl;  
        FileList (Full_file_path);  
            }else{Folder_files->push_back (Full_file_path); Std::cout << file_count << "" &LT;&LT  
        Full_file_path << Std::endl; } full_file_path.clear ();  
Empty directory} return folder_files;  
    int main (void) {std::shared_ptr<std::vector<std::string> > Folder_files;  
    Folder_files = FileList ("E:/picture/shoes3"); if (folder_files) {for (size_t i=0 i!= folder_files->size (); ++i) {std::cout << i+1 &  
        lt;< ":" << (*folder_files) [i] << Std::endl;  
} return 0; }

Author: csdn Blog spike_king

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/

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.