设为首页 加入收藏

TOP

用C语言获取任意文件的长度(可能大于2GB)(二)
2014-11-23 21:42:07 来源: 作者: 【 】 浏览:19
Tags:语言 获取 任意 文件 长度 可能 大于 2GB
i (大小约为16GB)的字节长度;如果在 Linux 下,尝试获取一个大文件 cn_dvd_532347.iso (大小约为2.5GB)的字节长度,经测试发现都能够正确得到结果。
[cpp]
/**************************************************
* File name: get_file_size.c
* Author: HAN Wei
* Author's blog: http://blog.csdn.net/henter/
* Date: Oct 31th, 2013
* Description: demonstrate how to invoke GetFileSize() function
**************************************************/
#include "get_file_size.h"
#include
#include
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。
首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C语言指针类型、指针大小、指针所.. 下一篇开关语句switch的几点说明

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: