• 正文
  • 相关推荐
申请入驻 产业图谱

飞凌嵌入式ElfBoard-文件的时间属性之futimens

18小时前
172
加入交流群
扫码加入
获取工程师必备礼包
参与热点资讯讨论

设置已打开文件的访问时间和修改时间(纳秒级别)。

1.头文件

#include <fcntl.h> /* 定义了一些常量 以AT_开头*/

#include <sys/stat.h>

2.函数原型

int futimens(int fd, const struct timespec times[2]);

3.参数

fd:文件描述符

times:是一个包含两个 struct timespec 的数组,第一个表示访问时间,第二个表示修改时间。可以将times任一数组元素的 tv_nsec 字段为如下数值:

⚫NULL:这时会将访问时间和修改时间都设置为当前时间。

⚫UTIME_NOW:则表示相应的时间戳设置为当前时间,此时忽略相应的 tv_sec 字段。

⚫UTIME_OMIT:则表示相应的时间戳保持不变,此时忽略 tv_sec 字段。

4.返回值

成功返回0,失败返回-1,并设置errno以指示返回错误类型 。

5.示例:(使用futimens修改已打开文件访问时间)

...

...

...

void update_file_time(const char *filename){

struct timespec times[2];

int i,fd;

fd = open(filename,O_RDONLY);

if (fd == -1) {

perror("open error");

exit -1;

}

for (i=0; i<2; i++) {

times[i].tv_sec = time(NULL);

times[i].tv_nsec = 666;

}

if (futimens(fd, times) == -1) {

perror("futimens");

close(fd);

exit(EXIT_FAILURE);

}

printf("File time updated successfully.n");

close(fd);

exit(0);

}

相关推荐