Read the file name under the folder in C + +, if there is a subfolder, recursively read the filename under the sub-file
1#include <fstream>2#include <iostream>3#include <string>4#include <sstream>5#include <vector>6#include <io.h>7 8 using namespacestd;9 voidGetallfiles (stringPath, vector<string>& files,stringpostfix)Ten { One //file Handle A Longhfile =0; - //file info. struct - struct_finddata_t FileInfo; the stringPATHP; - if(hfile = _findfirst (pathp.assign (Path). Append ("\\*"). C_str (), &fileinfo))! =-1) - { - Do + { - if((Fileinfo.attrib & _a_subdir))//is folder? + { A //is folder at if(strcmp (Fileinfo.name,".") !=0&& strcmp (Fileinfo.name,"..") !=0) - { - //files.push_back (p.assign (Path). Append ("/"). Append (Fileinfo.name));//Saving folder name -Getallfiles (pathp.assign (Path). Append ("/"). Append (Fileinfo.name), files, postfix);//find postfix file recursively - } - } in Else //Not folder - { to stringFilestr =Fileinfo.name; + stringPrepostfix ="."+postfix; - intIDX =Filestr.find (prepostfix); the stringPoststr =filestr.substr (idx, filestr.size ()); * if(0==Poststr.compare (prepostfix)) $Files.push_back (pathp.assign (Path). Append ("/"). Append (Fileinfo.name));Panax Notoginseng - } the} while(_findnext (hfile, &fileinfo) = =0);//Find Next + _findclose (hfile); A } the } + //Test - voidMain () $ { $ stringBasedir ="d:/video/t/"; - stringSavingname ="filename.txt"; - the stringPostfix ="Hik";//Specify Postfix -vector<string>files;Wuyi the getallfiles (basedir, files, postfix); - Wu intFileNum =files.size (); - About //saving as TXT file $Ofstream Outfilestream (savingname);//output File Stream -Outfilestream << FileNum <<Endl; - for(inti =0; i < FileNum; i++) - { AOutfilestream << Files[i] <<Endl; + } the outfilestream.close (); -}
C + + Read folder under file name