Text path Reading
In the pre-training of machine learning model, it is necessary to preprocess data, image, text and so on, and how to generate the text path of data quickly. This article will then use C + + to implement text path generation, you can find fixed format such as, such as .jpg
.txt
file path (absolute path or file name), and then save as .txt
text, convenient for later picture data read use.
The C + + code is implemented as follows:
#include <io.h> #include <fstreanm> #include <string> #include <vector> #include <iostream >using namespace Std;void getallfiles (string path, vector<string>& files, string format) {long hfile = 0; struct _finddata_t FileInfo; The structure used to store the file information, string p, if (hfile = _findfirst (p.assign (Path). Append ("\\*" + format). C_STR (), &fileinfo))!! =-1) First Find {do{//files.push_back (p.assign (Fileinfo.name));//save only file name Files.push_back (p.assign (path). App and ("\ \"). Append (Fileinfo.name)); Save file path and filename}while (_findnext (hfile, &fileinfo) = = 0); _findclose (hfile)} else if ((hfile = _findfirst (p.assign (Path). Append ("\\*"). C_str (), &fileinfo))! =-1) { do{if ((Fileinfo.attrib & _a_subdir))//If found is the folder {if (strcmp (fileinfo.name , ".") ! = 0 && strcmp (fileinfo.name, "...")! = 0)//Enter folder lookup {getallfiles (p.assign (path) . Append ("\ \ "). Append (Fileinfo.name), files, format); }} else//If the lookup is not a folder {//files.push_back (P.assign (Fileinfo.name));//Save Text Only The name Files.push_back (p.assign (path). Appand ("\ \"). Append (Fileinfo.name)); Save file path and filename}}while (_findnext (hfile, &fileinfo) = = 0); _findclose (hfile)}}int main () {string filepath = "D:\\path ..."; File root directory vector<string> files; Char *dstall = "Path.txt"; The file is read as jpg, string format = ". jpg"; Getallfiles (filepath, files, format); Ofstream ofn (Distall); int size = Files.size (); for (int i = 0; i<size; i++) {ofn<<files[i]<<endl;//write to file Cout<<files[i]<<endl ; Output to screen} ofn.close (); cout<< "File Number:" <<size<<endl; System ("pause";) return 0;}
Note : If the format assignment fails, it goes to the dead loop.
Related articles:
How to implement a browse get local file path
How C # uses the browse button to get the file path and folder path implementation method