CreateFile(), GetFileTime(), FileTimeToSystemTime(), SystemTimeToTzSpecificLocalTime()

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard and Windows

Additional library: Windows Platform SDK

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: non-CLR or unmanaged

To do: Another example of file timestamp

To show: Using CreateFile(), GetFileTime(), FileTimeToSystemTime(), SystemTimeToTzSpecificLocalTime() C functions

 

 

#include <windows.h>

#include <stdio.h>

 

int main(void)

{

// the files handle

HANDLE hFile1;

// FILETIME is another name for struct _FILETIME structure (a typedef)

FILETIME ftCreate;

// SYSTEMTIME is another name for struct _SYSTEMTIME structure (a typedef)

SYSTEMTIME stUTC, stLocal;

// low byte data

DWORD low;

// high byte data

DWORD high;

// filename, make sure the file is there

LPCWSTR fname1 = L"c:\\mytestfile.txt";

 

// Opening the existing file

hFile1 = CreateFile(fname1, //file to open

GENERIC_READ, //open for reading

FILE_SHARE_READ, //share for reading

NULL, //default security

OPEN_EXISTING, //existing file only

FILE_ATTRIBUTE_NORMAL, //normal file

NULL); //no attribute template

 

if(hFile1 == INVALID_HANDLE_VALUE)

{

printf("Could not open %S file, error %ul\n", fname1, GetLastError());

// just exit...

return 4;

}

else

printf("%S file opened successfully\n", fname1);

 

// Retrieve the created file times

if(!GetFileTime(hFile1, &ftCreate, NULL, NULL))

{

printf("Something wrong!\n");

return FALSE;

}

// Viewing the unreadable...

// Filing the 32 bit low part into variable low and another 32 bit high part into variable high

// Accessing the FILETIME structures' members, assigning them to some variables...

low = ftCreate.dwLowDateTime;

high = ftCreate.dwHighDateTime;

// Trying to display the content in hex...

printf("Unreadable format...\n");

printf("32 bit: low byte is = %0X and high byte is = %0X\n", low, high);

// Convert the file created time to local time.

FileTimeToSystemTime(&ftCreate, &stUTC);

SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);

printf("\nReadable format...\n");

// Build a readable string showing the date and time. Accessing the SYSTEMTIME structure's member

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);

// Accessing the SYSTEMTIME structures' members

printf("Local time format:\n");

printf("Created on: %02d/%02d/%d %02d:%02d\n", stLocal.wDay, stLocal.wMonth,

stLocal.wYear, stLocal.wHour, stLocal.wMinute);

// close the file's handle and itself

if(CloseHandle(hFile1) == 0)

printf("%S handle can't be closed! Error code: %d\n", fname1, GetLastError());

else

printf("%S handle closed successfully\n", fname1);

return 0;

}

 

Output example:

 

c:\mytestfile.txt file opened successfully

Unreadable format...

32 bit: low byte is = 496FD6C6 and high byte is = 1C71F6C

Readable format...

UTC System Time format:

Created on: 14/12/2006 10:40

Local time format:

Created on: 14/12/2006 18:40

c:\mytestfile.txt handle closed successfully

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index