如何在C ++中使用GetFileTime

XYZ*_*XYZ 3 c++ visual-c++

我正在尝试获取VC ++中文件的最后写入时间,但是我认为我以错误的方式写入文件句柄,您能帮我找到正确的写入文件句柄的方法吗,例如,我的目录为“ D:/ start 。文本”

这是我的代码

LPFILETIME ftWrite; 

HANDLE hFileMap;
hFileMap  = CreateFileMapping(L"D:/start.txt".txt", NULL, PAGE_READONLY, 0, 1, NULL);

GetFileTime(hFileMap, NULL, NULL, ftWrite);
Run Code Online (Sandbox Code Playgroud)

pro*_*mer 5

我尝试了这段代码,它起作用了。

#include <windows.h>
#include <stdio.h>    

int main(void)
{

    HANDLE hFile1;
    FILETIME ftCreate;
    SYSTEMTIME stUTC, stLocal;
    hFile1 = CreateFile("mytestfile.txt", GENERIC_READ, FILE_SHARE_READ,  NULL,  OPEN_EXISTING,  FILE_ATTRIBUTE_NORMAL, NULL);

    if(hFile1 == INVALID_HANDLE_VALUE)
    {
        printf("Could not open file, error %ul\n", GetLastError());
        return -1;
    }

    if(!GetFileTime(hFile1, &ftCreate, NULL, NULL))
    {
        printf("Something wrong!\n");
        return FALSE;
    }

    FileTimeToSystemTime(&ftCreate, &stUTC);
    printf("UTC System Time format:\n");
    printf("Created on: %02d/%02d/%d %02d:%02d\n", stUTC.wDay, stUTC.wMonth, stUTC.wYear, stUTC.wHour, stUTC.wMinute);

    return 0;

}
Run Code Online (Sandbox Code Playgroud)

干杯!!