【发布时间】:2019-02-06 01:24:24
【问题描述】:
我在 docker 中有一个应用程序,它是多线程的。我的转移时间代码如下。
FILE *fp = NULL;
char buf[256] = {0};
char filename[64] = {0};
long timestamp;
char status[16] = {0};
char tbuf[26] = {0};
fp = popen(cmd, "r");
if (fp) {
while (fscanf(fp, "%s %d %s", filename, ×tamp, status) != EOF) {
char *ts = ctime_r(×tamp, tbuf);
if (ts) {
ts[strlen(ts)-1] = '\0';
sprintf(buf, "%-32s%-32s%-8s", filename, ts, status);
} else {
sprintf(buf, "%-32s%-32s%-8s", filename, "Miss", status);
}
}
pclose(fp);
}
但是tbuf的内容是“Sat Oct 29 21:37:05 44614”,ts为NULL。
我认为时间戳是一个有效值。但是为什么失败了呢?
【问题讨论】:
-
ctime_r()是否期望long *或其他?见ref。如果没有完全启用编译器警告,那么您就是在浪费宝贵的时间。 -
“如果没有完全启用编译器警告,你就是在浪费宝贵的时间。”阿门。
-
发布 minimal reproducible example 包括包含文件、
main()、如何从文件中读取1535686785、文件内容以及如何确定“Sat Oct 29 21:37:05 44614”(打印)。这有助于快速找到解决方案。 -
@chux 不是ctime_r的错误,而是fscanf的错误。我使用 %d 来读取时间戳,所以只填充了时间戳的低位。我刚刚将时间戳设置为 0,现在一切正常。