遍历某文件夹下所有文件,并输出保存在txt

本文介绍了一种通过C++程序批量获取指定路径下所有.png图片文件名的方法,并将其记录到文本文件中,适用于HOG+SVM等算法训练数据准备。同时提供了两种遍历文件夹及其子文件夹的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include "io.h"
#include<fstream>
using namespace std;

ofstream MyFile("FileName.txt");

bool transfer(string fileName , int txtNum = 0)
{
	_finddata_t fileInfo;
	long handle = _findfirst(fileName.c_str(), &fileInfo);

	if (handle == -1L)
	{
		cerr<< "failed to transfer files" <<endl;
		return false;
	}

	do 
	{
		txtNum ++;
		MyFile<< fileInfo.name <<endl;
	} while (_findnext(handle, &fileInfo) == 0);
	cout<< " .txt files' number:  " << txtNum <<endl;;

	return true;
}

void main()
{
	//ofstream MyFile(FileName.txt);

	string FilePath="C:\\Users\\Administrator\\Desktop\\HOG+SVM\\数据集\\INRIAPerson\\INRIAPerson\\INRIAPerson\\96X160H96\\Train\\pos\\*.png";//注意通配符* ? 的用法,这个就是寻找XXX.png的文件,不管有多少个XXX, 如果是?.png,就是一个X,即X.png
	int num=0;

	transfer(FilePath,num);

}

我是为了运行hog+svm需要样本,下载了数据库,一个文件夹下全是.png的图片,需把每张图片名保存在txt供程序读取


另外这个帖子不错,有空看看 用类写的 http://blog.csdn.net/abcjennifer/article/details/18147551

我是参考这个帖子的:http://www.cnblogs.com/summerRQ/articles/2375749.html


2. 遍历文件夹及其子文件夹下所有文件。操作系统中文件夹目录是树状结构,使用深度搜索策略遍历所有文件。用到_A_SUBDIR属性,可运行程序如下:

void dfsFolder(string folderPath, ofstream &fout)
{
    _finddata_t FileInfo;
    string strfind = folderPath + "\\*";
    long Handle = _findfirst(strfind.c_str(), &FileInfo);
    
    if (Handle == -1L)
    {
        cerr << "can not match the folder path" << endl;
        exit(-1);
    }
    do{
        //判断是否有子目录
        if (FileInfo.attrib & _A_SUBDIR)    
        {
            //这个语句很重要
            if( (strcmp(FileInfo.name,".") != 0 ) &&(strcmp(FileInfo.name,"..") != 0))   
            {
                string newPath = folderPath + "\\" + FileInfo.name;
                dfsFolder(newPath, fout);
            }
        }
        else  
        {
            fout << folderPath << "\\" << FileInfo.name  << " ";
        }
    }while (_findnext(Handle, &FileInfo) == 0);

    _findclose(Handle);
    fout.close();
}

在判断有无子目录的if分支中,由于系统在进入一个子目录时,匹配到的头两个文件(夹)是"."(当前目录),".."(上一层目录)。需要忽略掉这两种情况。当需要对遍历到的文件做处理时,在else分支中添加相应的代码就好

PS:突然在网上看到一个绝技!

在许多样本的那个目录下新建一个文本txt,在其中输入如下:

dir /b/s/p/w *.png>train_list.txt
@pause

保存,改为.bat文件运行,就直接可以生成一个train_list.txt,里面包含目录


新增:

方法二:

利用Directory类实现文件夹中特定格式图像的遍历,Directory的头文件是windows.h。

#include<opencv2/opencv.hpp>
#include<iostream>
#include<vector>
#include<string>
#include <windows.h>

using namespace std;
using namespace cv;


void main()
{
	Directory dir;

	string path1 = "C:\\Users\\Administrator\\Desktop\\date\\MIT\\MIT人脸库\\faces";
	string  exten1 = "*.bmp";

	vector<string> filenames = dir.GetListFiles(path1, exten1, false);

	int size = filenames.size();

	for (int i = 0; i < size;i++)
	{
		cout << filenames[i] << endl;
	}
}
参见:http://blog.csdn.net/hei_ya/article/details/51387624


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值