Linux下struct dirent,DIR,struct stat使用例子

Linux下struct dirent,DIR,struct stat使用例子


以下例子,读取遍历目录中的文件夹,文件,并输出一些属性值,具体的相关结构体定义,可以参考:


http://www.liweifan.com/2012/05/13/linux-system-function-files-operation/

http://www.360doc.com/content/11/0110/16/4559801_85509847.shtml


/*
* linux struct dirent,DIR,struct stat
* date: 2015-7-16
* author: zhang
* compiled by gcc
*/

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

int main(int argc,char **argv)
{
	struct dirent *dirp; 
	struct stat buffer; 
	DIR *dp; //结构体,具体参阅http://www.liweifan.com/2012/05/13/linux-system-function-files-operation/
	
	if (argc != 2)
	{
		printf("argument is error!\n");
		return 0;
	}
	
	if ((dp = opendir(argv[1])) == NULL)
	{
		printf("file path is error!\n");
		return 0;
	}
	

	while(dirp = readdir(dp))
	{
		printf("filename: %s\n",dirp->d_name);//文件名
		stat(dirp->d_name,&buffer);
	//	printf("%d\n",stat(dirp->d_name,&buffer));
		printf("st_mode: %d\n",buffer.st_mode); //文件访问权限
		printf("st_uid: %d\n",buffer.st_uid);   //所有者用户识别号
		printf("st_mtime: %d\n",buffer.st_mtime);//最后一次修改该文件的时间
		printf("\n");
	
		
	}
	
	closedir(dp);
		
	return 0;
}

代码地址:


https://github.com/zhangxinxing/some_code_in_csdn/blob/master/struct_dirent.c


参考文献:


http://www.liweifan.com/2012/05/13/linux-system-function-files-operation/

http://www.360doc.com/content/11/0110/16/4559801_85509847.shtml

http://www.cplusplus.com/forum/unices/16005/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值