How do I get the size of file in disk? as show in the Windows explorer? e.g, a filename has:
如何在磁盘中获取文件的大小?如Windows资源管理器所示?e。g,文件名:
size: 172 bytes (172 bytes)
size on disk: 0 bytes
But my application shows:
但是我的应用程序显示:
GetDiskFreeSpace(L"C:\\" §orsPerCluster, &lpBytesPerSector,
&numberOfFreeClusters, &totalNumberOfClusters));
int TotalSize = lpBytesPerSector * ((fileLength + lpBytesPerSector - 1) / lpBytesPerSector);
printf("fileLength = %d\n", fileLength);
printf("TotalSize = %d\n", TotalSize);
it output:
输出:
fileLength = 172
TotalSize = 512
What am I missing to compute properly the size in disk of the filename?
要正确计算文件名在磁盘中的大小,我缺少什么?
2 个解决方案
#1
0
MSDN:
MSDN:
https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-getfilesize
https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-getfilesize
Some psuedo code:
推出一些伪代码:
HANDLE hFile = INVALID_HANDLE_VALUE; // call CreateFileA/W and Open the file
DWORD dwSize = 0;
GetFileSize(hFile, &dwSize); // check result of the call... if it failed call CloseHandle.
printf("GetFileSize() got: %d bytes");
Or if this does not work for you, You can always call NtQueryInformationFile and passing FileStandardInformation to FileInformationClass.
或者如果这对您不起作用,您可以调用NtQueryInformationFile并将FileStandardInformation传递给FileInformationClass。
#2
-2
You can try below code.
你可以试试下面的代码。
long filesize;
char *filename = "xyz.txt";
FILE *fp = fopen(filename, "r");
if(fp)
{
fseek(fp, 0L, SEEK_END);
filesize = ftell(fp);
fseek(fp, 0L, SEEK_SET);
printf("File size = %ld", filesize);
}
#1
0
MSDN:
MSDN:
https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-getfilesize
https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-getfilesize
Some psuedo code:
推出一些伪代码:
HANDLE hFile = INVALID_HANDLE_VALUE; // call CreateFileA/W and Open the file
DWORD dwSize = 0;
GetFileSize(hFile, &dwSize); // check result of the call... if it failed call CloseHandle.
printf("GetFileSize() got: %d bytes");
Or if this does not work for you, You can always call NtQueryInformationFile and passing FileStandardInformation to FileInformationClass.
或者如果这对您不起作用,您可以调用NtQueryInformationFile并将FileStandardInformation传递给FileInformationClass。
#2
-2
You can try below code.
你可以试试下面的代码。
long filesize;
char *filename = "xyz.txt";
FILE *fp = fopen(filename, "r");
if(fp)
{
fseek(fp, 0L, SEEK_END);
filesize = ftell(fp);
fseek(fp, 0L, SEEK_SET);
printf("File size = %ld", filesize);
}