标签:自动 日期 rowspan use nbsp 代码 int miss close
struct stat{mode_t st_mode; /* file type & mode (permissions) */ino_t st_ino; /* i-node number (serial number) */dev_t st_dev; /* device number (file system) */dev_t st_rdev; /* device number for special files */nlink_t st_nlink; /* number of links */uid_t st_uid; /* user ID of owner */gid_t st_gid; /* group ID of owner */off_t st_size; /* size in bytes, for regular files */struct timespec st_atim; /* time of last access */struct timespec st_mtim; /* time of last modification */struct timespec st_ctim; /* time of last file status change */blksize_t st_blksize; /* best I/O block size */blkcnt_t st_blocks; /* number of disk blocks allocated */};
struct timespec {time_t tv_sec; /* seconds */long tv_nsec; /* nanoseconds */};
| Field | Description | Example | ls(1) option |
| st_atim | last-access time of file data | read | -u |
| st_mtim | last-modification time of file data | write | 默认 |
| st_ctim | last-change time of i-node status | chmod、chown | -c |
| #include <sys/stat.h>
int futimens(int fd, const struct timespec times[2]); int utimensat(int fd, const char *path, const struct timespec times[2], int flag); Both return: 0 if OK, ?1 on error |
| #include <sys/time.h>
int utimes(const char *pathname, const struct timeval times[2]); Returns: 0 if OK, ?1 on error |
struct timeval {time_t tv_sec; /* seconds */long tv_usec; /* microseconds */};
/*** 文件内容:使用带O_TRUNC选项的open函数将文件长度截断为0,但并不更改其访问时间* 作者:firewaywei@126.com* 时间:2016年 11月 04日 星期五 22:00:14 CST*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <fcntl.h>int main(int argc, char *argv[]){int fd = 0;struct stat statbuf;struct timespec times[2];int i = 0;for (i = 1; i < argc; i++){if (stat(argv[i], &statbuf) < 0){printf("%s: stat error: %s\n", argv[1], strerror(errno));continue;}if (fd = open(argv[i], O_RDWR | O_TRUNC) < 0){printf("%s: open error: %s\n", argv[1], strerror(errno));continue;}times[0] = statbuf.st_atim;times[1] = statbuf.st_mtim;if (futimens(fd, times) < 0){printf("%s: futimens error: %s\n", argv[1], strerror(errno));}if (fd > 0){close(fd);}}exit(0);}
$ ls -l test test.c <------------ 查看长度和最后修改时间
-rwxrwxr-x 1 fireway fireway 9813 8月 14 18:14 test
-rw-rw-r-- 1 fireway fireway 475 8月 14 18:04 test.c
$ ls -lu test test.c <-------------查看最后访问时间
-rwxrwxr-x 1 fireway fireway 9813 8月 14 18:14 test
-rw-rw-r-- 1 fireway fireway 475 9月 9 22:44 test.c
$ date <---------------打印当天日期
2016年 11月 03日 星期四 07:54:21 CST
$ a.out test test.c <----------运行上面的程序
$ ls -l test test.c <--------------检查结果
-rwxrwxr-x 1 fireway fireway 0 11月 3 07:55 test
-rw-rw-r-- 1 fireway fireway 0 11月 3 07:55 test.c
$ ls -lu test test.c <----------------检查最后访问时间
-rwxrwxr-x 1 fireway fireway 0 8月 14 18:14 test
-rw-rw-r-- 1 fireway fireway 0 9月 9 22:44 test.c
$ ls -lc test test.c <-------------- 检查状态更改时间
-rwxrwxr-x 1 fireway fireway 0 11月 3 07:55 test
-rw-rw-r-- 1 fireway fireway 0 11月 3 07:55 test.c
标签:自动 日期 rowspan use nbsp 代码 int miss close
原文地址:http://www.cnblogs.com/fireway/p/6034263.html