dirop

本文介绍了一个C++实现的目录操作工具类DirOp,该类能够遍历指定目录并获取所有文件的完整路径。通过使用DirOp类,开发者可以轻松地获取目录下的所有文件信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/****************************************************************************
@File Name: DirOp.h
@Author: wangzhicheng
@mail: 2363702560@qq.com
@Created Time: Fri 10 Feb 2017 01:31:34 PM CST
****************************************************************************/
#ifndef DIR_OP_H
#define DIR_OP_H
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <string>
#include <vector>
#include <algorithm>
#include <thread>
namespace dirop
{
using namespace std;
class DirOp
{
private:
	string m_strDirPath;								// absolute path
public:
	DirOp(const string &path):m_strDirPath(path)
	{
	}
	/*
	 * @brief get all the file paths in the dir 
	 * */
	void GetFilePaths(vector<string>&paths);
private:
	/*
	 * @brief get all the file paths in the dir 
	 * */
	void GetFilePaths(const char *path, vector<string>&paths);
};
}
#endif

/****************************************************************************
@File Name: DirOp.cpp
@Author: wangzhicheng
@mail: 2363702560@qq.com
@Created Time: Fri 10 Feb 2017 01:31:34 PM CST
****************************************************************************/
#include "DirOp.h"
namespace dirop
{
/*
 * @brief get all the file paths in the dir 
 * */
void DirOp::GetFilePaths(vector<string>&paths)
{
	GetFilePaths(m_strDirPath.c_str(), paths);
}
/*
 * @brief get all the file paths in the dir 
 * */
void DirOp::GetFilePaths(const char *path, vector<string>&paths)
{
	DIR *dp = nullptr;
	struct dirent *dirp = nullptr;
	struct stat st;
	string newpath;
	static const string STR = "/"; 
	if(!(dp = opendir(path))) 
	{
		closedir(dp);
		dp = nullptr;
		fprintf(stderr, "%s can not be open...!", path);
		return;
	}
	while((dirp = readdir(dp))) 
	{
		if(!strcmp(dirp->d_name, ".") || !strcmp(dirp->d_name, "..")) continue;
		// generate a new path
		newpath = path;
		if('/' != newpath[newpath.size() - 1]) newpath += STR;		// judge the last character
		newpath += dirp->d_name;
		// stat the path
		if(stat(newpath.c_str(), &st))	
		{
			fprintf(stderr, "%s can not be stated...!\n", newpath.c_str());
			continue;
		}
		// is directory
		if(S_ISDIR(st.st_mode))
		{
			GetFilePaths(newpath.c_str(), paths);
		}
		else
		{
			paths.emplace_back(newpath);		// performance loss vector<string>&paths> -> string paths separated by blank character
			/*
			static const string STR = " ";
			char buf[256] = {0};
			string str;
//			memcpy(buf, newpath.c_str(), newpath.size());
			copy(begin(newpath), end(newpath), begin(str));
			str += STR;
			paths += str;
			*/
		}
	}
	closedir(dp);
}
}

/****************************************************************************
@File Name: test.cpp
@Author: wangzhicheng
@mail: wangzhicheng@qq.com
@Created Time: Fri 10 Feb 2017 02:04:46 PM CST
****************************************************************************/
#include <iostream>
#include <algorithm>
#include "DirOp.h"
using namespace dirop;
int main()
{
	vector<string>paths;
	DirOp dirop("/home");
	dirop.GetFilePaths(paths);
	for_each(begin(paths), end(paths), [](const string &path)
	{
		cout << path << endl;
	});

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值