How to quickly generate the text path of the data? C + + implements text path generation

Source: Internet
Author: User
Tags int size save file strcmp string format

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

Related Article

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.