C++遍历文件夹

目录

1. 只遍历当前一层文件夹下内容

2. 递归遍历当前文件夹下所有文件夹及子文件夹

3. 参考


1. 只遍历当前一层文件夹下内容

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

int run_main()
{
	// 图片所在文件夹,路径不要有中文
	std::string imgRoot = "D:/";

	_finddata64i32_t fileInfo;
	intptr_t hFile = _findfirst((imgRoot + "\\*.*").c_str(), &fileInfo);

	if (hFile == -1) {
		std::cout << "not find image!\n";
		return -1;
	}

	static int count = 0;

	do
	{
		std::string imgPath = imgRoot + "/" + std::string(fileInfo.name);

		std::cout << imgPath << std::endl;

		count++;
	} while (_findnext(hFile, &fileInfo) == 0);

	return EXIT_SUCCESS;
}

2. 递归遍历当前文件夹下所有文件夹及子文件夹

这里需要用到 c++ 17

#include <iostream>  
#include <vector>
#include <string>
#include <io.h>
#include <filesystem>

void curDir()
{
	std::string imgRoot = "D:/";
	using recursive_directory_iterator = std::filesystem::recursive_directory_iterator;
	for (const auto& dirEntry : recursive_directory_iterator(imgRoot))
	{
		std::string filePath = dirEntry.path().string();
		std::cout << filePath << std::endl;
	}
}

3. 参考

1.C++ 遍历文件夹_wthink0416的博客-CSDN博客

2.如何在标准C++中递归遍历每个文件/目录? |

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值