Dave's Brain

Browse - programming tips - windows get file size

Date: 2011oct6
Platform: Windows
Language: C/C++
Keywords: win32, win64

Q.  What's the best way to get the size of a file on Windows?

A.  If the file is not already open use GetFileAttributesEx()
It returns a 64 bit integer which supports files well over 4GB.

	LONGLONG FileSize(LPCTSTR szFileName, const LONGLONG lDefault = -1)
	{
		WIN32_FILE_ATTRIBUTE_DATA	info;
		LARGE_INTEGER			large;
	
		if (!GetFileAttributesEx(szFileName, GetFileExInfoStandard, &info))
		{
			return lDefault;
		}
	
		large.u.HighPart = info.nFileSizeHigh;
		large.u.LowPart = info.nFileSizeLow;
	
		return large.QuadPart;
	}

	void ExampleUse(LPCSTR szFile)
	{
		printf("Your file is %I64d bytes\n", FileSize(szFile));
	}

There is also a stat64() function.
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2012, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.
Advertisements: