要求将当前路径下,所有文件的权限及最后—次的访问时间提取出来,写入到file.txt中!!

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>

// 要求将当前路径下,
// 所有文件的权限及最后—次的访问时间提取出来,写入到file.txt中!!
int main(int argc, const char *argv[])
{
	//打开文件夹
	DIR* od = opendir("../fileIO_func/");
	if(od == NULL)
	{
		perror("opendir");
		return -1;
	}
	FILE* fo = fopen("./file.txt", "w");
    if (fo == NULL) {
        perror("fopen");
        return -1;
    }
	printf("文件打开成功!\n");

	//读取文件信息
	struct dirent* rd = NULL;
	while(1)
	{
		//判断读取是否成功
		rd = readdir(od);
		if(rd == NULL)
			break;
		if(rd->d_name[0] == '.')
			continue;
		printf("%s  ", rd->d_name);


		//获取文件属性
		struct stat stGet;
		if( stat("../fileIO_func/", &stGet) < 0  )
		{
			perror("stat");
			return -1;
		}
		mode_t qx = stGet.st_mode & 0777;

		//获取时间
		struct tm* info;
		info = localtime(&stGet.st_atime);

		fprintf(fo,"%s\n",rd->d_name);
		fprintf(fo,"%o\n",qx & 0777);
		fprintf(fo,"%04d-%02d-%02d %02d:%02d:%02d\n", info->tm_year+1900,
				info->tm_mon+1, info->tm_mday,info->tm_hour,info->tm_min,
				info->tm_sec);
	}

	//关闭文件夹
	if(closedir(od) < 0)
	{
		perror("closedir");
		return -1;
	}
	fclose(fo);
	printf("关闭成功!\n");
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值