博客
关于我
c++ 遍历文件夹中的文件
阅读量:144 次
发布时间:2019-02-27

本文共 2265 字,大约阅读时间需要 7 分钟。

_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 
#include
#include
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 
#include
#include
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 
#include
#include
#include
#include
#include
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 <
<

相关链接:

你可能感兴趣的文章
mabatis 中出现&lt; 以及&gt; 代表什么意思?
查看>>
Mac book pro打开docker出现The data couldn’t be read because it is missing
查看>>
MAC M1大数据0-1成神篇-25 hadoop高可用搭建
查看>>
mac mysql 进程_Mac平台下启动MySQL到完全终止MySQL----终端八步走
查看>>
Mac OS 12.0.1 如何安装柯美287打印机驱动,刷卡打印
查看>>
MangoDB4.0版本的安装与配置
查看>>
Manjaro 24.1 “Xahea” 发布!具有 KDE Plasma 6.1.5、GNOME 46 和最新的内核增强功能
查看>>
mapping文件目录生成修改
查看>>
MapReduce程序依赖的jar包
查看>>
mariadb multi-source replication(mariadb多主复制)
查看>>
MariaDB的简单使用
查看>>
MaterialForm对tab页进行隐藏
查看>>
Member var and Static var.
查看>>
memcached高速缓存学习笔记001---memcached介绍和安装以及基本使用
查看>>
memcached高速缓存学习笔记003---利用JAVA程序操作memcached crud操作
查看>>
Memcached:Node.js 高性能缓存解决方案
查看>>
memcache、redis原理对比
查看>>
memset初始化高维数组为-1/0
查看>>
Metasploit CGI网关接口渗透测试实战
查看>>
Metasploit Web服务器渗透测试实战
查看>>