CreateFile(), GetFileType(), GetFileSize(), 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: Showing file attributes - time and size

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


#include <windows.h>

#include <stdio.h>


int main(void)


// the files handle

HANDLE hFile1;

FILETIME ftCreate, ftAccess, ftWrite;

SYSTEMTIME stUTC, stLocal, stUTC1, stLocal1, stUTC2, stLocal2;

// the file is there...

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

// temporary storage for file sizes

DWORD dwFileSize;

DWORD dwFileType;


// 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


NULL); //no attribute template




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

return 4;



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


dwFileType = GetFileType(hFile1);

dwFileSize = GetFileSize(hFile1, NULL);


printf("%S size is %d bytes and file type is %d\n", fname1, dwFileSize, dwFileType);


// Retrieve the file times for the file.

if(!GetFileTime(hFile1, &ftCreate, &ftAccess, &ftWrite))


printf("Something wrong lol!\n");

return FALSE;


// Convert the created time to local time.

FileTimeToSystemTime(&ftCreate, &stUTC);

SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);

// Convert the last-access time to local time.

FileTimeToSystemTime(&ftAccess, &stUTC1);

SystemTimeToTzSpecificLocalTime(NULL, &stUTC1, &stLocal1);

// Convert the last-write time to local time.

FileTimeToSystemTime(&ftWrite, &stUTC2);

SystemTimeToTzSpecificLocalTime(NULL, &stUTC2, &stLocal2);

// Build a string showing the date and time.

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

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

printf("Last accessed: %02d/%02d/%d %02d:%02d\n", stLocal1.wDay, stLocal1.wMonth,

stLocal1.wYear, stLocal1.wHour, stLocal1.wMinute);

printf("Last written: %02d/%02d/%d %02d:%02d\n", stLocal2.wDay, stLocal2.wMonth,

stLocal2.wYear, stLocal2.wHour, stLocal2.wMinute);


// close the file's handle and itself

if(CloseHandle(hFile1) == 0)

printf("Can't close the %S handle!\n", fname1);


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

return 0;



Output example:


c:\mytestfile.txt file opened successfully

c:\mytestfile.txt size is 33 bytes and file type is 1

Created on: 14/12/2006 18:40

Last accessed: 16/12/2006 22:07

Last written: 16/12/2006 22:07

c:\mytestfile.txt handle closed successfully!

Press any key to continue . . .



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