Read all file names in a folder in C + +

Source: Internet
Author: User

// header file, note to add stdafx.h and io.h etc.  "stdafx.h"<io.h><vector> using  namespace std;

The following sub-function calculates all the files under the currently specified path folder (including files in the Iteration subfolder)

//Get child function of child file name//Path points to the folder paths to read//files is a string vector that stores file names within a foldervoidGetFiles (stringPath, vector<string>&files) {    //file handle    Longhfile =0; //File Information    struct_finddata_t FileInfo; stringp; if(hfile = _findfirst (p.assign (Path). Append ("\\*"). C_str (), &fileinfo))! =-1)    {         Do        {            //in the case of a directory, the iteration//if not, join the list            if((Fileinfo.attrib &_a_subdir)) {                if(strcmp (Fileinfo.name,".") !=0&& strcmp (Fileinfo.name,"..") !=0) getFiles (p.assign (Path). Append ("\\"). Append (Fileinfo.name), files); }            Else{files.push_back (p.assign (Path). Append ("\\"). Append (Fileinfo.name)); }        } while(_findnext (hfile, &fileinfo) = =0);    _findclose (hfile); }}

// called within the main function Char " D:\\wenjian ";  // home folder path    vector<string> files;  // Sub folder container    /// /Get all Files    under this path GetFiles (FilePath, files);

Speaking of file paths by the way, the file path declaration is generally char* path or char path[100], and it is possible to join the <string.h> string path

If you add a file name Files[i] to the path in the newly read files vector, then the assignment of the string can be made directly from the constructor of the string itself:

String path (files[i]);

The following code is not verified, simply complements the article integrity

Attach a section "read only the current folder name under a given path" (two versions)

Read all file names in a folder in C + +

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.