Unix/Linux 获取文件系统目录的程序。附一篇。

代码:

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <dirent.h>
#include <limits.h>

/* function type that is called for each filename */
typedef int Myfunc(const char *,const struct stat *,int);
static	Myfunc	myfunc;
static	int		myftw(char *,Myfunc *);
static	int		dopath(Myfunc *);

static	long	nreg,ndir,nblk,nchr,nfifo,nslink,nsock,ntot;

int main(int argc,char **argv)
{
	int		ret;

	if(argc != 2){
		fprintf(stderr,"usage: ftw <starting-pathname>\n");
		exit(1);
	}

	ret = myftw(argv[1],myfunc);	/* does it all */

	ntot = nreg + ndir + nblk + nchr + nfile + nslink + nsock;
	if(ntot == 0)	/* aoid divide by 0;print 0 for all counts */
		ntot = 1;
	printf("regular files = %7ld, %5.2f %%\n",nreg,nreg*100.0/ntot);
	printf("directories = %7ld, %5.2f %%\n",ndir,ndir*100.0/ntot);
	printf("block special = %7ld, %5.2f %%\n",nblk,nblk*100.0/ntot);
	printf("char special = %7ld, %5.2f %%\n",nchr,nchr*100.0/ntot);
	printf("FIFOs	 = %7ld, %5.2f %%\n",nfifo,nfifo*100.0/ntot);
	printf("symbolic links = %7ld, %5.2f %%\n",nslink,nslink*100.0/ntot);
	printf("sockets		= %7ld, %5.2f %%\n",nsock,nsock*100.0/ntot);

	exit(ret);
}

/*
 *	Descend through the hierarchy,starting at "pathname".
 *	The Caller's func() is called for every file.
*/
#define	FTW_F	1	/* File oter than directory */
#define	FTW_D	2	/* Directory */
#define	FTW_DNR	3	/* Directory that can't be read */
#define	FTW_NS	4	/* File that we can't stat */

static char *fullpath;	/* contains full pathname for every file */

static int myftw(char *pathname,Myfunc *func)
{
	int		len;
	fullpath = path_alloc(&len);	/* malloc's for PATH_MAX+1 bytes */

	strncpy(fullpath,pathname,len);	/* protect against */
	fullpath[len-1] = 0;			/* buffer overrun */

	return(dopath(func));
}

/*
 *	Descend through the hierarchy,starting at "fullpath".
 *	If "fullpath" is anything other than a directory,we lstat() it,
 *	call func(),and return. For a directory,we call ourself
 *	recursvely for each name in the directory.
*/
static int dopath(Myfunc *func)	/* we return whatever func() returns */
{
	struct	stat	statbuf;
	struct	dirent	*dirp;
	DIR		*dp;
	int		ret;
	char	*ptr;

	if(lstat(fullpath,&statbuf) < 0)	/* stat error */
		return(func(fullpath,&statbuf,FTW_NS));
	if(S_ISDIR(statbuf.st_mode) == 0)	/* not a directory */
		return(func(fullpath,&statbuf,FTW_F));
	
	/*
	 *	It's a dircetory.	First call func() for the directory,
	 *	then process each filename in the directory.
	*/
	if((ret = func(fullpath,&statbuf,FTW_D)) != 0)
		return (ret);

	ptr = fullpath + strlen(fullpath);	/* point to end of fullpath */
	*ptr++ = '/';
	*ptr = 0;

	if((dp = opendir(fullpath)) == NULL)	/* can't read directory */
		return(func(fullpath,&statbuf,FTW_DNR));

	while((dirp = readdir(dp)) != NULL){
		if(strcmp(dirp->d_name,".") == 0 || strcmp(dirp->d_name,"..") == 0)	/* ignore dot and dot-dot */
			continue;

		strcpy(ptr,dirp->d_name);	/* append name after slash */

		if((ret = dopath(func)) != 0)	/* recursive */
			break;	/* time to leave */
	}
	ptr[-1] = 0;	/* erase everything from alash onwards */

	if(closedir(dp) < 0){
		fprintf(stderr,"Can't close directory %s",fullpath);
		exit(-1);
	}

	return (ret);
}

static	int	myfunc(const char *pathname,const struct stat *statptr,int type)
{
	switch(type){
		case FTW_F:
			switch(statptr->st_mode & S_IFMT){
				case S_IFREG:	
					nreg++;
					break;
				case S_IFBLK:
					nblk++;
					break;
				case S_IFCHR:
					nchr++;
					break;
				case S_IFIFO:
					nfifo++;
					break;
				case S_IFLNK:
					nslink++;
					break;
				case S_IFSOCK:
					nsock++;
					break;
				case S_IFDIR:
					fprintf(stderr,"for S_IFDIR for %s\n",pathname);	/* directories should have type = FTW_D */
			}
			case FTW_D:
				ndir++;
				break;
			case FTW_DNR:
				fprintf(stderr,"Can't read directory %s\n",pathname);
				break;
			case FTW_NS:
				fprintf(stderr,"stat error for %s\n",pathname);
				break;				
			default:
				fprintf(stderr,"unknown type %d for pathname %s",type,pathname);
		}

		return 0;
	}
}

另一種列出目錄的demo,比較簡單,如下:

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <dirent.h>

/*
 *  功能描述:
 *	根據用戶輸入的目錄來逐級打印目錄名字
*/
void printdir(char *dir,int depth)
{
	DIR *dp;
	struct dirent *entry;
	struct stat statbuf;

	if((dp = opendir(dir)) == NULL){
		fprintf(stderr,"Can't Open directory: %s\n",dir);
		return;
	}
	chdir(dir);
	while((entry = readdir(dp)) != NULL){
		lstat(entry->d_name,&statbuf);
		if(S_ISDIR(statbuf.st_mode)){
			/* Found a directory,but we should ignore . and .. */
			if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0)
				continue;
			printf("%*s%s/\n",depth,"",entry->d_name);
			/* Recurse at a new indent level */
			printdir(entry->d_name,depth+4);
		}
		else
			printf("%*s%s\n",depth,"",entry->d_name);
	}
	chdir("..");	/* 返回上級目錄并關閉,可以看出,如果目錄深度很大的話, */
	closedir(dp);	/* 有可能會報錯,原因是單個進程所能打開的最大文件數 */
}

int main(int argc,char **argv)
{
	char *topdir=".";
	if(argc != 2){
		fprintf(stderr,"usage: ./a.out <pathname#>\n");
		exit(0);
	}
	topdir=argv[1];

	printf("Directory scan of %s:\n",topdir);
	printdir(topdir,0);
	printf("done.\n");

	exit(0);
}

其實這兩個例子的實質都是一樣的。
 

【顶级EI完整复现】【DRCC】考虑N-1准则的分布鲁棒机会约束低碳经济调度(Matlab代码实现)内容概要:本文介绍了名为《【顶级EI完整复现】【DRCC】考虑N-1准则的分布鲁棒机会约束低碳经济调度(Matlab代码实现)》的技术资源,聚焦于电力系统中低碳经济调度问题,结合N-1安全准则与分布鲁棒机会约束(DRCC)方法,提升调度模型在不确定性环境下的鲁棒性和可行性。该资源提供了完整的Matlab代码实现,涵盖建模、优化求解及仿真分析全过程,适用于复杂电力系统调度场景的科研复现与算法验证。文中还列举了大量相关领域的研究主题与代码资源,涉及智能优化算法、机器学习、电力系统管理、路径规划等多个方向,展示了广泛的科研应用支持能力。; 适合人群:具备一定电力系统、优化理论和Matlab编程基础的研究生、科研人员及从事能源调度、智能电网相关工作的工程师。; 使用场景及目标:①复现高水平期刊(如EI/SCI)关于低碳经济调度的研究成果;②深入理解N-1安全约束与分布鲁棒优化在电力调度中的建模方法;③开展含新能源接入的电力系统不确定性优化研究;④为科研项目、论文撰写或工程应用提供可运行的算法原型和技术支撑。; 阅读建议:建议读者结合文档提供的网资源,下载完整代码与案例数据,按照目录顺序逐步学习,并重点理解DRCC建模思想与Matlab/YALMIP/CPLEX等工具的集成使用方式,同时可参考文中列出的同类研究方向拓展研究思路。
内容概要:本文详细介绍了一个基于MATLAB实现的电力负荷预测项目,采用K近邻回归(KNN)算法进行建模。项目从背景意义出发,阐述了电力负荷预测在提升系统效率、优化能源配置、支撑智能电网和智慧城市建设等方面的重要作用。针对负荷预测中影响因素多样、时序性强、数据质量差等挑战,提出了包括特征工程、滑动窗口构造、数据清洗与标准化、K值与距离度量优化在内的系统性解决方案。模型架构涵盖数据采集、预处理、KNN回归原理、参数调优、性能评估及工程部署全流程,并支持多算法集成与可视化反馈。文中还提供了MATLAB环境下完整的代码实现流程,包括数据加载、归一化、样本划分、K值选择、模型训练预测、误差分析与结果可视化等关键步骤,增强了模型的可解释性与实用性。; 适合人群:具备一定MATLAB编程基础和机器学习基础知识,从事电力系统分析、能源管理、智能电网或相关领域研究的研发人员、工程师及高校师生;适合工作1-3年希望提升实际项目开发能力的技术人员; 使用场景及目标:①应用于短期电力负荷预测,辅助电网调度与发电计划制定;②作为教学案例帮助理解KNN回归在实际工程中的应用;③为新能源接入、需求响应、智慧能源系统提供数据支持;④搭建可解释性强、易于部署的轻量级预测模型原型; 阅读建议:建议结合MATLAB代码实践操作,重点关注特征构造、参数调优与结果可视化部分,深入理解KNN在时序数据中的适应性改进方法,并可进一步拓展至集成学习或多模型融合方向进行研究与优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值