加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

APUE之格式化time_t得到文件时间信息

发布时间:2020-12-15 16:23:53 所属栏目:安全 来源:网络整理
导读:vi 1.0.c #include stdio.h #include stdlib.h #include sys/stat.h #include time.h #define BUF 100 void print_time(time_t ct, char * s){ char st[BUF]; printf ( "%s: " ,s); strftime(st,BUF, "%Y %x %X" ,localtime(ct)); printf ( "%sn" ,st);} in

vi 1.0.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>
#define BUF 100


void print_time(time_t ct,char* s)
{
    char st[BUF];
    printf("%s: ",s);
    strftime(st,BUF,"%Y %x %X",localtime(&ct));
    printf("%sn",st);
}

int main(int argc,char* argv[])
{
    if (argc != 2)
    {
    printf("you need <pathname>n");
    exit(0);
    }

    struct stat statbuf;

    if (lstat(argv[1],&statbuf) < 0)
    {
    printf("lstat errorn");
    exit(0);
    }

    char *s = "文件最后访问时间为";
    print_time(statbuf.st_atime,s);

    s = "文件最后修改时间为";
    print_time(statbuf.st_mtime,s);

    s = "文件最后状态改变时间为";
    print_time(statbuf.st_ctime,s);
}

运行:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读