c++获取文件修改时间

#include <windows.h>
#include <stdio.h>
//----------- Error Handling Function -------------------
void error(LPSTR lpszFunction)
{
    CHAR szBuf[80];
    DWORD dw = GetLastError();

    sprintf(szBuf, "%s failed: GetLastError returned %u\n",
        lpszFunction, dw);

    MessageBox(NULL, szBuf, "Error", MB_OK);
    ExitProcess(dw);
}
//--------------------------------------------------------

BOOL GetFileTime(HANDLE hFile, LPSTR lpszCreationTime, LPSTR lpszLastAccessTime, LPSTR lpszLastWriteTime)
{
    FILETIME ftCreate, ftAccess, ftWrite;
    SYSTEMTIME stUTC1, stLocal1, stUTC2, stLocal2, stUTC3, stLocal3;

    // -------->获取 FileTime
    if (!GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite)){
        error("GetFileTime()");
        return FALSE;
    }
    //---------> 转换: FileTime --> LocalTime
    FileTimeToSystemTime(&ftCreate, &stUTC1);
    FileTimeToSystemTime(&ftAccess, &stUTC2);
    FileTimeToSystemTime(&ftWrite, &stUTC3);

    SystemTimeToTzSpecificLocalTime(NULL, &stUTC1, &stLocal1);
    SystemTimeToTzSpecificLocalTime(NULL, &stUTC2, &stLocal2);
    SystemTimeToTzSpecificLocalTime(NULL, &stUTC3, &stLocal3);

    // ---------> Show the  date and time.
    wsprintf(lpszCreationTime, "创建时间:\t%02d/%02d/%d  %02d:%02d",
        stLocal1.wDay, stLocal1.wMonth, stLocal1.wYear,
        stLocal1.wHour, stLocal1.wMinute);
    wsprintf(lpszLastAccessTime, "最后访问时间:\t%02d/%02d/%d  %02d:%02d",
        stLocal2.wDay, stLocal2.wMonth, stLocal2.wYear,
        stLocal2.wHour, stLocal2.wMinute);
    wsprintf(lpszLastWriteTime, "最后修改时间:\t%02d/%02d/%d  %02d:%02d",
        stLocal3.wDay, stLocal3.wMonth, stLocal3.wYear,
        stLocal3.wHour, stLocal3.wMinute);
    return TRUE;
}
//----------------------------------------------------------------
int main(int argn, char* argv[])
{
    if (argn < 2)
    {
        return -1;
    }

    HANDLE hFile;
    TCHAR szCreationTime[30], szLastAccessTime[30], szLastWriteTime[30];
    hFile = CreateFile(argv[1], 0, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    GetFileTime(hFile, szCreationTime, szLastAccessTime, szLastWriteTime);
    if (hFile == INVALID_HANDLE_VALUE){
        error("GetLastWriteTime()");
        return 0;
    }
    printf("%s\n%s\n%s\n", szCreationTime, szLastAccessTime, szLastWriteTime);
    CloseHandle(hFile);
    system("pause");

    return 0;
}

 

转载于:https://www.cnblogs.com/autumoonchina/p/5852265.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值