// 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 + +