c++读取一个文件目录下的指定类型的文件名

本文介绍如何使用C++编程从一个文件目录中获取特定类型的文件名。核心代码段展示了实现这一功能的关键步骤。

关键代码

void getAllFiles(string path, vector<string>& files, string fileType) {
//fileType:文件后缀;files:存放所有文件名的栈
	intptr_t hFile = 0;
	_finddata_t  fileInfo;
	string p;
	if ((hFile = _findfirst(p.assign(path).append("\\*" + fileType).c_str(), &fileInfo)) != -1) {
		do {
			files.push_back(p.assign(path).append("\\").append(fileInfo.name));
		} while (_findnext(hFile, &fileInfo) == 0);
		_findclose(hFile);
	}

}
#include <iostream>
#include <fstream>
#include <io.h>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char **argv)
//int main( )
{
	vector<string> files;
	string extr = ".jpg"; //指定文件类型为jpg
	getAllFiles(argv[1], files,extr);
	for (int i = 0; i < files.size(); i++) {
		cout << files[i] << endl;
	}
}
<think>好的,用户想用C++读取指定目录下的所有文件名。首先,我需要回忆C++中处理目录的方法。记得在C++17之前,标准库没有直接的文件系统操作功能,通常需要借助平台特定的API,比如Windows的FindFirstFile/FindNextFile或者Linux的dirent.h。不过,C++17引入了<filesystem>库,这应该是最新且跨平台的解决方案。 用户需要一个示例,所以我应该优先展示使用C++17 filesystem的方法。首先,检查编译器是否支持C++17,用户可能需要加上-std=c++17的编译选项。然后,代码示例需要包含必要的头文件,比如<filesystem>和<iostream>,并使用namespace fs = std::filesystem简化代码。 接下来,遍历目录中的条目,可以使用directory_iterator。对于每个条目,需要检查是否是常规文件或者目录,然后输出文件名。需要注意的是,用户可能想排除隐藏文件,比如以点开头的文件,这在Linux系统中常见。不过用户的问题没有特别提到,所以可能不需要过滤,但作为扩展可以考虑。 另外,用户提供的引用中有提到Perl例子排除以点开头的文件,可能用户对过滤文件名有兴趣,所以在示例之后可以提到如何修改代码来过滤特定文件,比如使用filename().string().find('.') == 0来跳过隐藏文件。 此外,还需要考虑异常处理,比如目录不存在的情况,使用try-catch块来捕获filesystem_error异常,并提示错误信息。 如果用户不能使用C++17,可能需要回退到平台特定的方法,比如Linux的dirent.h或Windows的WinAPI。这部分可以作为备选方案提供,但主要推荐使用标准库的filesystem,因为更现代和跨平台。 在代码示例中,路径应该由用户指定,比如替换成自己的路径。同时,注意跨平台路径分隔符的问题,使用/即可,filesystem会处理。 最后,生成相关问题,可能涉及性能优化、递归遍历子目录、排除特定文件类型等,这些都是用户可能进一步关心的点。</think>在C++读取目录文件名的现代方法主要通过`<filesystem>`库实现(C++17标准)。以下是两种实现方式及示例代码: 一、使用C++17标准库方法 ```cpp #include <iostream> #include <filesystem> namespace fs = std::filesystem; int main() { std::string path = "/path/to/directory"; // 替换为实际路径 try { for (const auto& entry : fs::directory_iterator(path)) { // 输出文件名(不含路径) std::cout << entry.path().filename() << '\n'; // 输出完整路径:entry.path().string() /* 类型判断示例 if (entry.is_regular_file()) std::cout << " [File]"; else if (entry.is_directory()) std::cout << " [Dir]"; */ } } catch (const fs::filesystem_error& e) { std::cerr << "Error: " << e.what() << '\n'; } return 0; } ``` 编译时需添加C++17支持标志:`-std=c++17 -lstdc++fs`(gcc/clang) 二、传统跨平台方法(C++17前) ```cpp #include <iostream> #include <dirent.h> void listFiles(const char* path) { DIR* dir = opendir(path); if (!dir) { perror("opendir failed"); return; } while (dirent* entry = readdir(dir)) { // 过滤.和.. if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue; std::cout << entry->d_name << '\n'; } closedir(dir); } int main() { listFiles("/path/to/directory"); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值