如何在多个操作系统下获取c中的文件修改时间?

发布于 2025-01-06 12:08:52 字数 107 浏览 5 评论 0原文

我正在尝试用 c 语言编写一个可移植函数来比较两个文件的最后修改时间。这些文件很小,并且是一个接一个地写入的,因此我需要比 1 秒(毫秒)更精细的粒度。
似乎有太多的时间/日期功能......

I'm trying to write a portable function in c that compares the last modified times of 2 files. The files are tiny and written one right after the other, so I need finer granularity than 1 second (milliseconds).
There seems to be a plethora of time/date functions...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

腻橙味 2025-01-13 12:08:52

您应该查看 stat() 函数。它可以在 *nixWindows

它们都会返回一个包含字段名 st_msize 的结构。它们是我听说过的从操作系统获取此类信息的最好的功能。

由于您需要可移植性,因此请注意 Windows 上可用的各种不同类型。在 *NIX 上,它是一个经典的 time_t 结构。如果包含特定调用,则可以获得纳秒 mtime:根据手册页,它是在 POSIX.1-2008 中定义的。

您还可以查看如何处理64/32位time_t

You should look to the stat() function. It's available on *nix and on windows.

They will both return you a struct containing a field name st_msize. They are the finest functions I have heard of in order to get this kind of information from an Operating System.

Since you need portability, beware to take care of the various different types available on Windows. On *NIX, it's a classic time_t structure. If you include specific call, you can obtain nano seconds mtime: it was defined in POSIX.1-2008, according to the man page.

You can also take a look at how you can deal with 64/32 bit time_t

一世旳自豪 2025-01-13 12:08:52

C 标准没有任何这方面的功能,但 Posix 规范有。 2008 年版本甚至提供亚秒级时间戳。 #define _POSIX_C_SOURCE 200809L

以下代码应该让您了解如何使用它。

#define _POSIX_C_SOURCE 200809L
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include <stdio.h> // for printf
#include <stdlib.h> // for EXIT_FAILURE

int main(int argc, char **argv)
{
    for (int i = 1; i < argc; ++i) {
        struct stat st = {0};
        int ret = lstat(argv[i], &st);
        if (ret == -1) {
            perror("lstat");
            return EXIT_FAILURE;
        }

        printf("%s: mtime sec=%lld nsec=%lld\n", argv[i],
               (long long) st.st_mtim.tv_sec, 
               (long long) st.st_mtim.tv_nsec);
    }

    return 0;
}

The C standard does not have any functions for this, but the Posix specification does. The 2008 edition even provides sub-second timestamps. #define _POSIX_C_SOURCE 200809L

The following code should give you an idea how to use it.

#define _POSIX_C_SOURCE 200809L
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include <stdio.h> // for printf
#include <stdlib.h> // for EXIT_FAILURE

int main(int argc, char **argv)
{
    for (int i = 1; i < argc; ++i) {
        struct stat st = {0};
        int ret = lstat(argv[i], &st);
        if (ret == -1) {
            perror("lstat");
            return EXIT_FAILURE;
        }

        printf("%s: mtime sec=%lld nsec=%lld\n", argv[i],
               (long long) st.st_mtim.tv_sec, 
               (long long) st.st_mtim.tv_nsec);
    }

    return 0;
}
三生路 2025-01-13 12:08:52

对于 POSIX UNIX,stat() 是可移植的,并给出struct stat st_mtime,它是以纪元秒为单位的修改时间。 Windows stat 返回 Windows 时间值,并且具有创建时间而不是 st_ctime
对于非 POSIX UNIX 实现、Windows 和其他操作系统,没有文件修改时间的可移植概念。因此,根据您对便携式的想法,整个概念可能适合您,也可能不适合您。

For POSIX UNIX, stat() is portable and gives struct stat st_mtime which is the modification time in epoch seconds. Windows stat returns windows time values, and has creation time rather than st_ctime.
For non-POSIX UNIX implementations, Windows and other OSes there is no portable concept of file modification time. So, depending on your idea of portable, this whole concept may or my not work for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文