【问题标题】:C program to change the files timestamp in linux [duplicate]C程序在linux中更改文件时间戳[重复]
【发布时间】:2022-01-15 16:38:56
【问题描述】:

我正在编写一个 C 程序来更改 linux 中文件的修改时间戳(时间和日期)。我找到了命令,但我正在寻找一种可以通过 C 程序执行此操作的方法。有什么办法可以做到吗?

【问题讨论】:

    标签: c linux datetime timestamp


    【解决方案1】:

    linux试试

    int utimensat(int dirfd, const char *pathname,
                  const struct timespec times[2], int flags);
    

    或旧命令

    int utime(const char *filename, const struct utimbuf *times);
    

    【讨论】:

      【解决方案2】:

      可以,通过使用system() 函数和touch Linux 命令。来自system() manual page

      int system(const char *command);

      system() 库函数使用...执行 command

      中指定的 shell 命令

      编辑:这会将时间戳更新为当前时间和日期。

      【讨论】:

      • 绝对没有理由使用system() 来更新时间戳
      • 我发现这是完成任务的直接方法。如果可以添加,有什么更好的方法?
      • 无需启动 shell 进程来运行命令,只需使用相关的系统调用即可。另一个答案提到了几个选项。
      • 这很简单,但在 CPU 时间方面也相当昂贵。不是一个错误的答案,但也不是最有效的。大多数应用程序都将从 Bo 的解决方案中受益。
      • 很公平,但我认为没有必要投反对票。也许 OP 想要一些快速运行的东西,而不需要过多地使用 struct timespec 和标志。
      猜你喜欢
      • 1970-01-01
      • 2021-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-19
      • 1970-01-01
      • 1970-01-01
      • 2016-05-06
      相关资源
      最近更新 更多