将文件夹中的图像路径自动生成txt文件(便于opencv遍历处理图像)

代码:

#include<iostream>
#include<vector>
#include<io.h>
#include<fstream>

using namespace std;

ofstream off("img_pow_sta.txt", ios::out);
vector<int> number;

int num = 0;

void getFiles(string path, vector<string>& files)
{
    //文件句柄  
    long   hFile = 0;
    //文件信息  
    struct _finddata_t fileinfo;
    string p;
    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) {
                    getFiles(p.assign(path).append("\\").append(fileinfo.name), files);
                    num++;
                }
            }
            else
            {
                files.push_back(p.assign(path).append("\\").append(fileinfo.name));
                number.push_back(num);
            }
        } while (_findnext(hFile, &fileinfo) == 0);
        _findclose(hFile);
    }
}

int main() {
    char* filepath = "D:\\Desktop\\people\\people";
    vector<string> files;
    getFiles(filepath, files);
    char str[30];
    int size = files.size();
    for (int i = 1; i < size; i++) {
        off << files[i].c_str();
        off << " ";
        //off << number[i];
        off << "\n";
    }
    off.close();
    return 0;
}

 

posted on 2017-06-13 15:15  在某一天老去  阅读(1252)  评论(0编辑  收藏  举报

导航