gpt4 book ai didi

c - fstat : st_atime and st_mtime not a member?

转载 作者:行者123 更新时间:2023-11-30 15:11:27 24 4
gpt4 key购买 nike

我正在做一个fstat在我的文件描述符上并将其转储到 struct stat 。我阅读了 fstat 的文档(链接如下),它声称有成员 st_atimest_mtime .

http://pubs.opengroup.org/onlinepubs/009695399/basedefs/sys/stat.h.html

GCC 让我进行编译,但是单步执行 GDB,我无法打印出这些成员(即我可以打印所有其他成员)。 GDB 声称它们不存在。

事实上,当我打印出struct stat时, st_atime拼写为st_atim (即与 st_mtime 相同)。然后它看起来像是一个元组或其他东西,因为它包含两个值, tv_sectv_nsec .

有谁知道为什么GDB声称它们不存在?

还有,有谁知道如何将其传递给 memcpy ?我正在使用C90 .

这是它提示说我无法通过 time_t 的代码行在这里。我该如何转换它才能使这条线发挥作用?

memcpy(&temp.otar_adate, file_statistics.st_atime, OTAR_DATE_SIZE);

我的操作系统:CentOS

最佳答案

在 Linux 上,至少某些版本,st_atimestruct stat 中的一些其他时间字段位于 struct timespec 内,并包含正确的时间戳全纳秒精度。在这些系统上,st_atime 是对其他内容的定义。在我的 CentOS 机器上,它被定义为 st_atim.tv_sec

将您的代码放入预处理器中以查看它在您的系统上的内容:

$ cat foo.c
#include <sys/stat.h>

void
foo(void)
{
struct stat st;

(void)st.st_atime;
}
$ cc -E foo.c | tail -7
void
foo(void)
{
struct stat st;

(void)st.st_atim.tv_sec;
}

Gdb 不知道预处理器定义,因此它无法知道您的代码是如何进行预处理的。它只知道结构体的真正定义。

关于c - fstat : st_atime and st_mtime not a member?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35645959/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com