Windows程序设计之GetFileSize函数获取文件大小的方法

一、GetFileSize函数

DWORD GetFileSize(
  HANDLE hFile,           // handle to file
  LPDWORD lpFileSizeHigh  // high-order word of file size
);

二、实现代码

#include <Windows.h>
#include <iostream>

int main(void)
{
	HANDLE hFile = CreateFile(TEXT(__FILE__), GENERIC_READ, FILE_SHARE_READ,
		NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

	if (hFile == INVALID_HANDLE_VALUE)
	{
		std::cout << "打开文件失败: " << GetLastError() << std::endl;
		return 1;
	}

	DWORD dwFileSizeHigh, dwFileSizeLow;
	dwFileSizeLow = GetFileSize(hFile, &dwFileSizeHigh);

	std::cout << (((long long)dwFileSizeHigh << 32) | dwFileSizeLow) << std::endl;

	return 0;
}

Leave a Reply

Your email address will not be published.