_utime、_utime32、_utime64、_wutime、_wutime32、_wutime64

设置文件修改时间。

int _utime(
   const char *filename,
   struct _utimbuf *times 
);
int _utime32(
   const char *filename,
   struct __utimbuf32 *times 
);
int _utime64(
   const char *filename,
   struct __utimbuf64 *times 
);
int _wutime(
   const wchar_t *filename,
   struct _utimbuf *times 
);
int _wutime32(
   const wchar_t *filename,
   struct __utimbuf32 *times 
);
int _wutime64(
   const wchar_t *filename,
   struct __utimbuf64 *times 
);

参数

  • filename
    指向字符串的指针包含路径和文件名。

  • times
    存储时间值的指针。

返回值

如果文件修改时间更改,这些函数都返回 0。 返回值 -1 指示一个错误。 如果传递一个无效参数为,则调用无效参数的处理程序,如 参数验证 中所述。 如果允许继续执行,这些函数返回 -1,并且 errno 设置为以下任一值:

  • EACCES
    指定目录路径或只读文件

  • EINVAL
    Invalid times argument

  • EMFILE
    太多打开文件 (必须打开文件更改其修改时间)

  • ENOENT
    路径或文件名未找到。

有关这些内容的详细信息以及其他返回代码,请参见 _doserrno、errno、_sys_errlist 和 _sys_nerr

如果更改日期在1970 年 1 月 1 日午夜,则文件日期可能更改,在使用函数的结束日期前。 _utime 和 _wutime 使用 64 位值,因此,在结束日期时间为 23:59: 59,3000 年 12 月 31 日,UTC。 如果 _USE_32BIT_TIME_T 被定义强制旧行为,结束日期为 03:14: 07 年 1 月 19 日 2038 年,UTC。 _utime32 或 _wutime32 使用 32 位时类型忽略 _USE_32BIT_TIME_T 是否被定义,因而总是有更早的结束日期。 _utime64 或 _wutime64 始终使用 64 位时类型,因此,这些函数始终支持较晚的结束日期。

备注

_utime 函数通过 filename设置指定文件的修改时间*。*进程必须具有访问文件的写权限才能更改时间。 在 Windows 操作系统,您可以在 _utimbuf 结构里更改时间访问和修改时间。 如果 times 是一个 NULL 指针,修改时间设置为当前的本地时间。 否则,times 必须指向 _utimbuf 类型的结构,定义在SYS\UTIME.H。

_utimbuf 存储文件权限和修改时间用 _utime 更改文件的修改日期。 结构具有以下字段,为 time_t类型:

  • actime
    文件访问时间

  • modtime
    文件修改时间

特定版本的 _utimbuf 结构 (_utimebuf32 和 __utimbuf64) 被用 32 位和 64 位的时间类型版本版本定义。 这些用在 32 位和 64 位版本特定生成函数。 除非 _USE_32BIT_TIME_T 定义,默认情况下_utimbuf 使用 64 位时类型。

_utime 与 _futime 相同,除了 _utime 的 filename 参数为文件名或一个文件路径,而不是打开的文件文件说明符。

_wutime 是 _utime 的宽字符版本;_wutime 的 filename 参数是宽字符字符串。 否则这些函数具有相同行为。

一般文本例程映射

TCHAR.H 例程

未定义的 _UNICODE & 和 _MBCS

已定义 _MBCS

已定义 _UNICODE

_tutime

_utime

_utime

_wutime

_tutime32

_utime32

_utime32

_wutime32

_tutime64

_utime64

_utime64

_wutime64

要求

例程

必需的标头

可选标头

_utime, _utime32, _utime64

<sys/utime.h>

<errno.h>

_utime64

<sys/utime.h>

<errno.h>

_wutime

<utime.h> 或 <wchar.h>

<errno.h>

有关其他兼容性信息,请参见“简介”中的兼容性

示例

本程序使用 _utime 修改设置文件修改时间为当前时间。

// crt_utime.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/utime.h>
#include <time.h>

int main( void )
{
   struct tm tma = {0}, tmm = {0};
   struct _utimbuf ut;

   // Fill out the accessed time structure
   tma.tm_hour = 12;
   tma.tm_isdst = 0;
   tma.tm_mday = 15;
   tma.tm_min = 0;
   tma.tm_mon = 0;
   tma.tm_sec = 0;
   tma.tm_year = 103;

   // Fill out the modified time structure
   tmm.tm_hour = 12;
   tmm.tm_isdst = 0;
   tmm.tm_mday = 15;
   tmm.tm_min = 0;
   tmm.tm_mon = 0;
   tmm.tm_sec = 0;
   tmm.tm_year = 102;

   // Convert tm to time_t
   ut.actime = mktime(&tma);
   ut.modtime = mktime(&tmm);

   // Show file time before and after
   system( "dir crt_utime.c" );
   if( _utime( "crt_utime.c", &ut ) == -1 )
      perror( "_utime failed\n" );
   else
      printf( "File time modified\n" );
   system( "dir crt_utime.c" );
}

示例输出

Volume in drive C has no label.
 Volume Serial Number is 9CAC-DE74

 Directory of C:\test

01/09/2003  05:38 PM               935 crt_utime.c
               1 File(s)            935 bytes
               0 Dir(s)  20,742,955,008 bytes free
File time modified
 Volume in drive C has no label.
 Volume Serial Number is 9CAC-DE74

 Directory of C:\test

01/15/2002  12:00 PM               935 crt_utime.c
               1 File(s)            935 bytes
               0 Dir(s)  20,742,955,008 bytes free

.NET Framework 等效项

不适用。若要调用标准 C 函数,请使用 PInvoke。有关更多信息,请参见平台调用示例

请参见

参考

时间管理

asctime、_wasctime

ctime、_ctime32、_ctime64、_wctime、_wctime32、_wctime64

_fstat、_fstat32、_fstat64、_fstati64、_fstat32i64、_fstat64i32

_ftime、_ftime32、_ftime64

_futime、_futime32、_futime64

gmtime、_gmtime32、_gmtime64

localtime、_localtime32、_localtime64

_stat、_wstat 函数

time、_time32、_time64