#else
if (fseeko(fp, (long long)(0), SEEK_END))
{
#ifdef _DEBUG
printf("fseek() function failed!\n");
#endif
fclose(fp);
return (-1);
}
*file_byte_size=ftello(fp);
/***********************/
#endif
fclose(fp);
return 0;
}
第 3 个文件
这个文件的功能是:如果在 Windows 下,尝试获取一个大文件 RedHat62.vdi (大小约为16GB)的字节长度;如果在 Linux 下,尝试获取一个大文件 cn_dvd_532347.iso (大小约为2.5GB)的字节长度,经测试发现都能够正确得到结果。
[cpp]
/**************************************************
* File name: get_file_size.c
* Author: HAN Wei
* Author's blog
* Date: Oct 31th, 2013
* Description: demonstrate how to invoke GetFileSize() function
**************************************************/
#include "get_file_size.h"
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
#if defined(_WIN32) || defined(_WIN64)
char file_name[256]="f:\\myvdisk\\RedHat62.vdi";
#else
char file_name[256]="/media/0009-EB9C/cn_dvd_532347.iso";
#endif
long long file_byte_length;
int error_code;
if ( error_code=GetFileSize(file_name, &file_byte_length) )
{
printf("get file length failed!\n");
#if defined(_WIN32) || defined(_WIN64)
system("pause");
#endif
return 1;
}
else
printf("file %s length is %lld bytes.\n", file_name, file_byte_length);
#if defined(_WIN32) || defined(_WIN64)
system("pause");
#endif
return 0;
}
获取任意文件(不受 2GB 大小限制)长度还有其他的方法:例如在 Linux 平台上可以使用 stat() 函数,该 函数返回的结构体 stat 中包含一个成员变量 st_size,它表示文件的字节长度,类型为 off_t.
在 Visual Studio 2005 及以后版本的开发工具中提供了 _stat64() 函数,该函数返回的结构体 _stat64 中包含一个成员变量 st_size,它表示文件的字节长度,类型为 __int64.