C库使用(一)——文件时间获取并转换
<sys/stat.h>库文件下的时间属性默认是以秒为单位的,需要通过ctime函数进行转换,头文件为<ctime.h>。
代码如下所示
/*
* @filename: level4_d3.c
* @FilePath: \C Task\level4_d3.c
* @Descripttion: 遍历文件夹下所有文件,并打印文件名、大小、日期等属性
* @version: V1.0
* @Author:
* @Date: 2020-12-06 16:49:30
* @LastEditors: Bi Jiaqin
* @LastEditTime: 2020-12-06 16:50:22
*/
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <time.h>
int main(int argc, const char *argv[])
{
DIR *dir;
int fd, f_num = 1;
struct dirent *dent;
struct stat buf;
dir = opendir(".");
if(dir)
{
while((dent = readdir(dir)) != NULL)
{
stat(dent->d_name, &buf);
printf("编 号:%d \n文 件 名: %s \n大 小:%d \n创建时间:%s修改时间:%s访问时间:%s\n\n\n",f_num, dent->d_name,int)buf.st_size,ctime(&buf.st_ctime),ctime(&buf.st_mtime),ctime(&buf.st_atime));
f_num++;
}
}
return 0;
}

浙公网安备 33010602011771号