如何在Mac OS X下的C中设置文件的创建日期? [英] How to set the creation date of a file in C under Mac OS X?

查看:185
本文介绍了如何在Mac OS X下的C中设置文件的创建日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mac OS X存储文件创建时间,我知道如何使用stat()<sys/stat.h>读取文件.

Mac OS X stores file creation time, and I know how to read it with stat() from <sys/stat.h>.

我找不到一种方法来设置C语言中的创建时间.由于实用程序SetFile可以做到(因此,SetFile是Apple命令行工具包的一部分),因此一定有可能实现:

I could not find a way, how to set the creation time in C. It must be possible somehow, as the utility SetFile can do it (SetFile is part of the command line tools package from Apple):

SetFile -d '12/31/1999 23:59:59' file.txt

如何在C语言中完成?

推荐答案

您可以使用 utimes .

如果times为非NULL,则假定它指向两个timeval的数组 结构.访问时间设置为第一个元素的值, 并将修改时间设置为第二个元素的值.

If times is non-NULL, it is assumed to point to an array of two timeval structures. The access time is set to the value of the first element, and the modification time is set to the value of the second element.

并且:

对于 支持文件生成(创建)时间(例如UFS2)的文件系统, 如果第二个元素的出生时间设置为第二个元素的值 元素早于当前设置的出生时间.既要诞生 时间和修改时间,需要两个调用;第一个设置 出生时间和第二次设置(可能是较新的)修改 时间

For file systems that support file birth (creation) times (such as UFS2), the birth time will be set to the value of the second element if the second element is older than the currently set birth time. To set both a birth time and a modification time, two calls are required; the first to set the birth time and the second to set the (presumably newer) modification time

例如:

struct timeval times[2];
memset(times, 0, sizeof(times));

times[0].seconds = 946684799;  /* 31 Dec 1999 23:59:59 */
times[1].seconds = 946684799;

utimes("/path/to/file", &times);

如果传递的修改时间早于文件的当前创建时间,则将设置创建时间.如果要设置其他修改时间,则可以再次调用utimes.

If the modification time passed is older than the current creation time of the file, the creation time will be set. You can then call utimes again if you want to set a different modification time.

这篇关于如何在Mac OS X下的C中设置文件的创建日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆