c++ 遍历文件夹中的文件

_finddata_t 结构体

struct _finddata_t 是用来存储文件各种信息的结构体。
定义如下:

struct _finddata_t
{
unsigned attrib;
time_t time_create;
time_t time_access;
time_t time_write;
_fsize_t size;
char name[_MAX_FNAME];
};

各成员变量的含义:
unsigned attrib:
文件属性的存储位置。它存储一个unsigned单元,用于表示文件的属性。
文件属性是用位表示的,主要有以下一些:

_A_ARCH(存档)
_A_HIDDEN(隐藏)
_A_NORMAL(正常)
_A_RDONLY(只读)
_A_SUBDIR(文件夹)
_A_SYSTEM(系统)
遍历文件夹下的文件:
#include <stdio.h>
#include <io.h>
#include <string>

int main()
{
    //目标文件夹路径
    std::string inPath = "C:\\Program Files\\*";//遍历文件夹下的所有文件
    //用于查找的句柄
    long handle;
    struct _finddata_t fileinfo;
    //第一次查找
    handle = _findfirst(inPath.c_str(),&fileinfo);
    if(handle == -1)
        return -1;
    do
    {
        //找到的文件的文件名
        printf("%s\n", fileinfo.name);

    } while (!_findnext(handle,&fileinfo));

    _findclose(handle);
    system("pause");
    return 0;
}

这里写图片描述

遍历文件夹下的指定类型文件

遍历文件夹下的所有.jpg文件:

#include <stdio.h>
#include <io.h>
#include <string>

int main()
{
    //目标文件夹路径
    std::string inPath = "E:\\image\\image\\*.jpg";//遍历文件夹下的所有.jpg文件
    //用于查找的句柄
    long handle;
    struct _finddata_t fileinfo;
    //第一次查找
    handle = _findfirst(inPath.c_str(),&fileinfo);
    if(handle == -1)
        return -1;
    do
    {
        //找到的文件的文件名
        printf("%s\n", fileinfo.name);

    } while (!_findnext(handle,&fileinfo));

    _findclose(handle);
    system("pause");
    return 0;
}

这里写图片描述

遍历文件夹中的图片并重新按顺序命名输出:

遍历文件夹中的图片,将读取到的图片重新按顺序命名输出;同时创建文件名列表(txt或dat文件)。

#include <stdio.h>
#include <io.h>
#include <string>
#include <iostream>  
#include <fstream> 
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;

int main()
{
    _finddata_t FileInfo; 
    //读取图片所在的路径
    string inPath = "E:\\image\\face\\negitive\\img\\"; 
    string strfind = inPath + "*";
    long Handle = _findfirst(strfind.c_str(), &FileInfo);
    char filename[300];
    Mat src;
    int k = 0;
    //输出txt文件(路径列表)所在的路径
    ofstream outfile("E:\\bg.txt", ofstream::app); 
    //输出dat文件(路径列表)所在的路径
    //ofstream outfile("E:\\bg.dat", ofstream::app); 
    if (Handle == -1L)
    {  
        cerr << "can not match the folder path" << endl;  
        exit(-1);
    }
    //namedWindow("input", WINDOW_AUTOSIZE);
    do{
        //判断是否有子目录  
        if (FileInfo.attrib & _A_SUBDIR)  
        {  
            if ((strcmp(FileInfo.name, ".") != 0) && (strcmp(FileInfo.name, "..") != 0))  
            {  
                inPath += FileInfo.name;
                cout <<inPath<<endl;  
            }
        }
        else
        {
            cout <<inPath<<FileInfo.name<<endl;
            //读取图片所在的路径
            string path = "E:/image/face/negitive/img/" + (string)FileInfo.name;
            src = imread(path);
            //显示读取的图片
            //imshow("input", src);
            //输出文件的路径
            sprintf_s(filename,"E:/image/face/negitive/img0/%d.jpg",k);
            //将读取到的图片重新按顺序命名输出
            imwrite(filename, src);
            outfile<<"img/"<<k<<".jpg\n";
            k++;
            waitKey(1);
        }  
    } while (_findnext(Handle, &FileInfo) == 0);  
    _findclose(Handle);   
    return 0;  
}

相关链接:
百度百科:_finddata_t结构体

  • 18
    点赞
  • 103
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值