- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
对于我的计算机科学课,我们正在 C 程序中实现“ls”功能,并且需要使用 st_mtime 字段。然而,当我使用 struct stat 时,它只有一个 st_mtim 字段,而不是我需要的 st_mtime 字段。这与我在/usr/include/sys/stat.h 的头文件中看到的相符。如何获得包含我需要的字段的结构体定义?
最佳答案
我在我的系统 (Debian) 上查看了这个。
由于某些原因,st_mtime
被定义为一个宏;定义是 st_mtim
。
忽略 header 的内容(它们对编译器的意义比对人类读者的意义更大),只需按照文档进行操作即可。 man 2 stat
会告诉您需要包含哪些 header ,至少在我的系统上它显示了一个示例程序。
血淋淋的细节(你不需要知道就可以正确使用它):
在 /usr/include/bits/stat.h
中,struct stat
类型定义了以下成员(以及其他成员):
struct timespec st_atim; /* Time of last access. */
struct timespec st_mtim; /* Time of last modification. */
struct timespec st_ctim; /* Time of last status change. */
struct timespec
是一个结构,其中包含一个名为 tv_sec
的 time_t
类型的成员。 (其他成员允许更高分辨率的时间戳。)
接下来是以下预处理器指令:
# define st_atime st_atim.tv_sec
# define st_mtime st_mtim.tv_sec
# define st_ctime st_ctim.tv_sec
所以你可以在自己的代码中直接引用foo.st_mtime
,它会扩展为foo.st_mtim.tv_sec
,也就是time_t
你需要的对象。
更新:
st_atim
等人的声明之前(在我当前的 Ubuntu 18.04 系统上)有以下评论:
/* Nanosecond resolution timestamps are stored in a format
equivalent to 'struct timespec'. This is the type used
whenever possible but the Unix namespace rules do not allow the
identifier 'timespec' to appear in the <sys/stat.h> header.
Therefore we have to handle the use of this header in strictly
standard-compliant sources special. */
关于c++ - 为什么我的 struct stat 有一个 st_mtim 而不是 st_mtime 字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23944199/
我有一个程序想要检查文件是否已被修改。 regtest 失败,事实上,尽管文件已更改,st_mtime 却没有!此外,外部统计数据也证实了这一点。 我相信 st_mtime 应该改变,因为 stat(
为什么这个程序在 Ubuntu 中构建时会产生语法错误? #include "stdio.h" #include "stdlib.h" #include "string.h" #inc
我正在尝试在 Cygwin 中使用 Python 3.8 获取文件的最后修改时间。 所以如果我执行 stat .profile 我会得到: File: .profile Size: 1236
我正在做一个fstat在我的文件描述符上并将其转储到 struct stat 。我阅读了 fstat 的文档(链接如下),它声称有成员 st_atime和st_mtime . http://pubs.
我在使用msync时遇到一个问题。非常感谢您的帮助! 简而言之,我映射文件A,并修改它和msync,但st_mtime没有改变。即使munmap文件A并退出进程,st_mtime也不变。 以下是代码。
我想从结构stat中显示我的两个属性 struct stat { dev_t st_dev; /* ID of device containing file */ ino_t
在此示例中,我使用的是在 OSX shell 中使用 pyenv 安装的 Python 3.6.5。 我一直在尝试一些概念证明文件监视代码,我认为使用文件当前和上次测量的增量 st_mtime 足以“
从 stat() 调用检索到的修改时间转换为 datetime 对象的最惯用/最有效的方法是什么?我想出了以下(python3): from datetime import datetime, tim
当我使用 O_CREAT 标志在 UNIX 中创建或打开文件时,st_mtime、st_ctime 和 st_atime文件发生变化。但是,当我使用 O_TRUNC 标志创建或打开文件时,只有 st_
我需要将 st_mtime 转换为字符串格式以将其传递给 java 层,我尝试使用此示例 http://www.cplusplus.com/forum/unices/10342/但是编译器产生错误 i
对于我的计算机科学课,我们正在 C 程序中实现“ls”功能,并且需要使用 st_mtime 字段。然而,当我使用 struct stat 时,它只有一个 st_mtim 字段,而不是我需要的 st_m
我是一名优秀的程序员,十分优秀!