Linux 环境下使用C++ 提取指定文件夹及其子文件夹中的文件名信息

本文介绍了如何在Linux环境下使用C++,通过包含dirent.h头文件,遍历指定文件夹及其子文件夹,提取并保存所有文件的名称到vector容器中。
摘要由CSDN通过智能技术生成

1. 关键是#include <dirent.h>头文件中包含的一些关于文件操作的属性。可以将文件夹及子文件夹中的文件信息提取出来保存在vector<string>中

/*
环境: LInux
*/
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>
#include <iostream>
#include <time.h>
#include <vector>
#include <algorithm>
using namespace std;
//文件夹下文件信息及其子文件夹中的文件信息
void listDir(const char *name, vector<string> &fileNames, bool lastSlash);
int main()
{
	string directoryPath="/root/testDir/";
	vector<string> fileNames;
	listDir(directoryPath.c_str(),fileNames,true);
	
	cout<<"total files: "<<fileNames.size()<<endl;
	cout<<fileNames[0]<<endl;
	return 0;
}
void listDir(const char *name, vector<string> &fileNames, bool lastSlash)
{
	DIR *dir;
    struct dirent *entry;
    struct stat statbuf;
    struct tm      *tm;
    ti
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值