Get a File Time

Get a File Time

以下的範例示範如何用 c 取得一個檔案的建立時間

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
 
void main( void )
{
    struct _stat buf;
    int result;
    struct tm when;
   
    /* Get data associated with "stat.c": */
    result = _stat( "filetime.c", &buf );
 
    /* Check if statistics are valid: */
    if( result != 0 )
        perror( "Problem getting information" );
    else
    {
        when = *localtime( &buf.st_ctime );
 
        /* Output some of the statistics: */
        printf( "File size     : %ld\n", buf.st_size );
        printf( "Drive         : %c:\n", buf.st_dev + 'A' );
        printf( "Time modified : %s", ctime( &buf.st_ctime ) );
       
        //取得個別時間成份
        printf("Year:%d\n", when.tm_year + 1900);
        printf("Month:%d\n", when.tm_mon + 1);
        printf("Day:%d\n", when.tm_mday);
        printf("Hour:%d\n", when.tm_hour);
        printf("Minute:%d\n", when.tm_min);
        printf("Second:%d\n", when.tm_sec);
    }
}

 

若要取得「最後存取時間」,將「buf.st_ctime」改成「buf.st_atime」;
若要取得「最後修改時間」,將「buf.st_ctime」改成 「buf.st_mtime」。

 

Dotblogs 的標籤: , ,