文本路径读取

在机器学习模型训练前期,需要对数据、图像、文本等进行预处理,而如何快速生成数据的文本路径呢?本文接下来直接使用C++实现文本路径生成,可查找固定格式如.jpg.txt 等文件路径(绝对路径或文件名),然后保存为.txt 文本,方便后期图片数据读取使用。


C++代码实现如下:

#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; //用来存储文件信息的结构体    string p;    if(hfile = _findfirst(p.assign(path).append("\\*" + format).c_str(), &fileinfo)) != -1) //第一次查找    {        do{            //files.push_back(p.assign(fileinfo.name)); //只保存文件名files.push_back(p.assign(path).appand("\\").append(fileinfo.name)); //保存文件路径和文件名        }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(strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0) //进入文件夹查找                {                    GetAllFiles(p.assign(path).append("\\").append(fileinfo.name), files, format);                }            }            else //如果查找的不是文件夹            {                //files.push_back(p.assign(fileinfo.name)); //只保存文件名    files.push_back(p.assign(path).appand("\\").append(fileinfo.name)); //保存文件路径和文件名            }        }while(_findnext(hfile, &fileinfo) == 0);        _findclose(hfile)    }}int main(){    string filepath = "D:\\path..."; //文件根目录    vector<string> files;    char *dstAll = "path.txt";    //读取所以格式为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; //写入文件        cout<<files[i]<<endl; //输出到屏幕    }    ofn.close();    cout<<"file number: "<<size<<endl;    system("pause";)    return 0;}

注意:如果format赋值出错会进入死循环。

相关文章:

如何实现浏览获取本地文件路径

C#如何使用浏览按钮获得文件路径和文件夹路径的实现方法

更多相关文章

  1. 用C++实现最短路径之Dijkstra算法
  2. 云原生 DevOps 的 5 步升级路径
  3. 项目里文件名永远不要用中文!永远不要!
  4. Python+Kepler.gl轻松制作酷炫路径动画
  5. Java 类路径(学习 Java 编程语言 036)
  6. LeetCode 实战:「图解」二叉树中的最大路径和
  7. 动画:面试必刷之二叉树中和为某一值的路径
  8. 使用位运算处理一道难题:获取所有钥匙的最短路径
  9. 数据结构与算法——图最短路径

随机推荐

  1. android常用UI控件的使用例子
  2. Android能用Linux打败Linux手机吗?
  3. Android中实现滑动的七种方式总结
  4. 大虾们的 android 浅谈
  5. android 解决输入法键盘遮盖布局问题
  6. android学习笔记(2)---第一个Android应用程
  7. android 在配置文件中指定上级activity
  8. Android调用WebService系列之KSoap2对象
  9. Android缺乏整体控制或成发展障碍
  10. 如何在eclipse的android工程里引用androi