如何更新時間戳文件--utimes()簡單介紹

這兩天遇到一個問題,需要設置一個時間戳文件,並且需要在需要的時候更新該時間戳文件,並比較上一次更新時間以及此次更新時間的差值,並打印出來。

下面是簡單的步驟,相關代碼如下:

#include <utime.h>
#include <time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#define TIME_STAMP "/root/charliye/time_flag"
main()
{
  struct stat buf1;
  struct stat buf2;
  time_t t_old,t_old1,t_now,t_new;
  stat(TIME_STAMP , &buf1); //獲取時間戳文件的相關信息存到buf1
  t_old = buf1.st_mtime;
  t_old1 = buf1.st_atime;
  printf("the last modify time is : %u,%u\n", t_old,t_old1);
  t_now = time(NULL);
  printf("the current time is : %u\n", t_now);
  int ret = utimes(TIME_STAMP, NULL); //更新時間戳文件爲當前時間
  stat(TIME_STAMP, &buf2); //獲取當前時間戳文件的相關信息存到buf2
  t_new = buf2.st_mtime;
  printf("the new modify time is : %u\n", t_new);
}
程序運行結果如下:

[root@localhost charliye]# ./utime
the last modify time is : 1456380013,1456380013
the current time is : 1456380015
the new modify time is : 1456380015
[root@localhost charliye]# ./utime
the last modify time is : 1456380015,1456380015
the current time is : 1456380021
the new modify time is : 1456380021
[root@localhost charliye]# ./utime
the last modify time is : 1456380021,1456380021
the current time is : 1456380031
the new modify time is : 1456380031
[root@localhost charliye]# ./utime
the last modify time is : 1456380031,1456380031
the current time is : 1456380044
the new modify time is : 1456380044






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章