共用方式為


_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
    out 包含此路徑或檔案名稱之字串的指標。

  • 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 位元時間型別,因此這些函式永遠支援更以後的結束日期。

備註

檔案的修改時間由 filename指定的 _utime 函式集*。*處理序必須具有檔案的寫入以變更時間。 在 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