c - 用于打印 “stat”缓冲区内容的格式说明符

标签 c linux unix operating-system stat

我在Linux上使用了 stat()函数来检索有关文件的详细信息。
详细信息之一是存储在变量“ st_atime ”中的上次访问时间
但是显示这种细节的格式说明符是什么。我的程序不断抛出错误。

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

int main()
{
  struct stat buf;
  stat("reversi.py",&buf);
  printf("The size  is...%d\n",buf.st_atime);
  return 0;
}

错误是
warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘__time_t’ [-Wformat=]
     printf("The size  is...%d\n",buf.st_atime);

此类数据的正确格式说明符是什么?
函数还返回了更多详细信息。是否有地方可以找到所有适合这些详细信息的格式说明符?
谢谢。

最佳答案

ctime() gmtime() localtime()函数均采用 time_t 数据类型。

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

int main()
{
    struct stat buf;
    stat("1.c",&buf);
    printf("Last Access was :  %s\n",ctime(&buf.st_atime));
    return 0;
}

这将打印
Last Access was :  Tue Apr 28 10:09:15 2015

关于c - 用于打印 “stat”缓冲区内容的格式说明符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30098362/

相关文章:

linux - 将变量值插入 : separator 列

c - C Unix 中的段错误(核心转储)

regex - 在文件名中插入字符

c++ - 我可以在 Mac 上使用 C++ 获得 -1 退出代码吗?

C:小端

c - 需要有 Do/While 循环来使用 NOT 运算符来处理带有 Switch 语句的字符

c - 指向函数指针的指针

c - 使用外部 : getting 'file not found'

c - 我的系统调用无法正常工作

linux - 通过 bash 命令注释以查看 pkill