- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个程序想要检查文件是否已被修改。 regtest 失败,事实上,尽管文件已更改,st_mtime
却没有!此外,外部统计数据也证实了这一点。
我相信 st_mtime 应该改变,因为 stat(2) 说
The field st_mtime is changed by file modifications, for example, by mknod(2), truncate(2), utime(2) and write(2) (of more than zero bytes).
这里有一些 C 代码可以说明这个问题:
#include <assert.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
void touch(const char *fn, const char *contents)
{
FILE *fp;
assert(fp = fopen(fn, "w"));
fprintf(fp, contents);
fclose(fp);
}
int main( int argc, char *argv[] )
{
struct stat st;
char path[] = "/tmp/foo";
time_t m1, m2;
unsigned int t;
touch(path, "hello\n");
assert(!stat(path, &st));
m1 = st.st_mtime;
touch(path, "hello, world!\n");
t = sleep(2);
assert(!stat(path, &st));
m2 = st.st_mtime;
printf("Sleep remaining: %lu\n", t);
printf("Elapsed modtime=%lu\n", m2 - m1);
}
这里有一些东西可以提供给 bash 以确认它不仅仅是在 C 程序中缓存:
$ while true; do stat /tmp/foo | grep Modify; sleep 1;done
有什么建议吗? Fwiw,这是在这样识别的系统上运行的:
jeff@london:src $ uname -a
Linux london 2.6.32-37-generic #81-Ubuntu SMP Fri Dec 2 20:32:42 UTC 2011 x86_64 GNU/Linux
jeff@london:src $
最佳答案
首先,我强烈建议您不要在 assert
中使用带有副作用的语句。我知道这很方便,如果代码永远不会投入生产也没关系,但这不是好的形式。特别是,如果您定义了 NDEBUG
,assert
将转换为 noop,条件代码永远不会执行。
现在,问题很可能是您将 sleep
调用放在了两个 touch
调用之后,而不是在它们之间。改变这个:
touch(path, "hello\n");
assert(!stat(path, &st));
m1 = st.st_mtime;
touch(path, "hello, world!\n");
t = sleep(2);
assert(!stat(path, &st));
m2 = st.st_mtime;
到
touch(path, "hello\n");
int rv = stat(path, &st);
assert(!rv);
m1 = st.st_mtime;
t = sleep(2);
touch(path, "hello, world!\n");
int rv = stat(path, &st);
assert(!rv);
m2 = st.st_mtime;
关于c++ - 写入文件,st_mtime 不变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8749543/
我有一个程序想要检查文件是否已被修改。 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
我是一名优秀的程序员,十分优秀!