#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;
}
2024.2.29
最新推荐文章于 2024-10-16 19:08:56 发布