How to get the size of a file using the Win32 API in C

1 Answer

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

int main(void)
{
	HANDLE hFile;
	LPCWSTR fname = L"d:\\data.txt";
	DWORD dwFileSize;

	hFile = CreateFile(fname, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 
		                                                           FILE_ATTRIBUTE_NORMAL, 
																   NULL); 

	if (hFile == INVALID_HANDLE_VALUE)
		printf("Could not open %S file, error %d\n", fname, GetLastError());
	else
	{
		dwFileSize = GetFileSize(hFile, NULL);
		printf("%S size is %d bytes\n", fname, dwFileSize);
	}

	return 0;
}


/*
run:

d:\data.txt size is 5 bytes

*/

 



answered Jun 16, 2015 by avibootz
edited Jan 30 by avibootz
...