How to set the modification time of a file programmatically?

后端 未结 5 719
余生分开走
余生分开走 2021-01-05 07:48

How do I set the modification time of a file programmatically in Windows?

5条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-05 08:50

    From: http://rosettacode.org/wiki/File/Modification_Time#C

    #include 
    #include 
    #include 
    
    const char *filename = "input.txt";
    
    int main() {
      struct stat foo;
      time_t mtime;
      struct utimbuf new_times;
    
      stat(filename, &foo);
      mtime = foo.st_mtime; /* seconds since the epoch */
    
      new_times.actime = foo.st_atime; /* keep atime unchanged */
      new_times.modtime = time(NULL);    /* set mtime to current time */
      utime(filename, &new_times);
    
      return 0;
    }
    

提交回复
热议问题