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;
}


% % % clear; clc; data_path = fullfile(pwd, '..', filesep, "data", filesep); addpath(data_path); lib_path = fullfile(pwd, '..', filesep, "lib", filesep); addpath(lib_path); code_path = genpath(fullfile(pwd, '..', filesep, 'MKC_LKRGD')); addpath(code_path); dirop = dir(fullfile(data_path, '*.mat')); datasetCandi = {dirop.name}; % datasetCandi = {'COIL20_1440n_1024d_20c_uni.mat','binaryalphadigs_1404n_320d_36c_uni.mat',... % 'mfeat_pix_2000n_240d_10c_uni.mat','FACS_v2_Trachea-counts_1013n_13741d_7c_uni.mat',... % 'MNIST_4000n_784d_10c_uni.mat','Macosko_6418n_8608d_39c_uni.mat'}; datasetCandi = {'binaryalphadigs_1404n_320d_36c_uni.mat'}; exp_n = 'LKRGDF'; % profile off; % profile on; for i1 = 1 : length(datasetCandi) data_name = datasetCandi{i1}(1:end-4); dir_name = [pwd, filesep, exp_n, filesep, data_name]; try if ~exist(dir_name, 'dir') mkdir(dir_name); end prefix_mdcs = dir_name; catch disp(['create dir: ',dir_name, 'failed, check the authorization']); end clear X y Y; load(data_name); if exist('y', 'var') Y = y; end if size(X, 1) ~= size(Y, 1) Y = Y'; end assert(size(X, 1) == size(Y, 1)); nSmp = size(X, 1); nCluster = length(unique(Y)); %********************************************************************* % LKRGDF %********************************************************************* fname2 = fullfile(prefix_mdcs, [data_name, '_12k_LKRGDF.mat']); if ~exist(fname2, 'file') Xs = cell(1,1); Xs{1} = X; Ks = Xs_to_Ks_12k(Xs); Ks2 = Ks{1,1}; Ks = Ks2; clear Ks2 Xs; [nSmp, ~, nKernel] = size(Ks); nRepeat = 10; k_range = [5,10,15,20]; t_range = [3,5,7,9]; paramCell = LKRGDF_build_param(k_range, t_range); nParam = length(paramCell); LKRGDF_result = zeros(nParam, 1, nRepeat, 10); LKRGDF_time = zeros(nParam, 1); for iParam = 1:nParam%算法执行 disp(['LKRGDF iParam= ', num2str(iParam), ', totalParam= ', num2str(nParam)]); fname3 = fullfile(prefix_mdcs, [data_name, '_12k_LKRGDF_', num2str(iParam), '.mat']); if exist(fname3, 'file') load(fname3, 'result_10_s', 'tt'); LKRGDF_time(iParam) = tt; for iRepeat = 1:nRepeat LKRGDF_result(iParam, 1, iRepeat, :) = result_10_s(iRepeat, :); end else param = paramCell{iParam}; opts = []; opts.k = param.k; opts.t = param.t; tic; [A, beta, Ss] = LKRGDF(Ks, nCluster, opts);% A2 = ( abs(A) + abs(A') ) / 2 ; CKSym = BuildAdjacency(thrC(A2,0.7)); H_normalized = SpectralClustering_ncut(CKSym,nCluster); tt = toc; LKRGDF_time(iParam) = tt; result_10_s = zeros(nRepeat, 10); for iRepeat = 1:nRepeat label = kmeans(H_normalized, nCluster, 'emptyaction', 'singleton', 'replicates', 100, 'display', 'off'); result_10 = my_eval_y(label, Y); LKRGDF_result(iParam, 1, iRepeat, :) = result_10'; result_10_s(iRepeat, :) = result_10'; end save(fname3, 'result_10_s', 'tt', 'param'); end end a1 = sum(LKRGDF_result, 2); a3 = sum(a1, 3); a4 = reshape(a3, size(LKRGDF_result,1), size(LKRGDF_result,4)); LKRGDF_grid_result = a4/nRepeat; LKRGDF_result_summary = [max(LKRGDF_grid_result, [], 1), sum(LKRGDF_time)/nParam]; save(fname2, 'LKRGDF_result', 'LKRGDF_grid_result', 'LKRGDF_time', 'LKRGDF_result_summary'); disp([data_name, ' has been completed!']); end end rmpath(data_path); rmpath(lib_path); rmpath(code_path); % profile viewer;给每行代码给出注释
07-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值