用C语言获取任意文件的长度(可能大于2GB)

来源:互联网 发布:grub2手动引导ubuntu 编辑:程序博客网 时间:2024/05/01 09:04

    用C语言获取文件长度的常见思路是:

    打开文件后用 fseek() 函数把文件位置指针移动到文件的末尾,用 ftell() 获得这时位置指针距文件头的字节数,这个字节数就是文件的长度。但是这样做也会受到下面的限制:ftell() 函数的返回值是 long 类型,在Windows 下(不管是 32 位还是 64 位),long 类型变量的长度都是 4 字节,这样能正常获取到的文件长度必须小于 2GB。

    微软在 Visual Studio 2005 及以后版本的开发工具中,在 stdio.h 中提供了函数 _fseeki64() 和 _ftelli64()。_fseeki64() 函数中表示文件指针偏移量的变量是 __int64 类型,_ftelli64() 的返回值也是 __int64 类型,因此可以用这两个函数获取大文件(指超过 2GB)的长度。

   注意 Linux 和 Unix 中的 C 编译器没有提供 _fseeki64() 和 _ftelli64() 函数,但是在 stdio.h 中提供了 fseeko() 和 ftello() 函数,fseeko() 函数中表示文件指针的偏移量的变量是 off_t 类型,ftello() 的返回值也是off_t 类型,off_t 类型的长度有可能是 32 比特或 64 比特,使用宏定义 #define _FILE_OFFSET_BITS 64 可以将 off_t 设为64 比特长。

   下面给出用C语言获取文件长度(可能大于 2GB)的实现示例,代码在 Windows 和 Linux 平台上都能使用(未在 Unix 上测试),包括三个文件:

第 1 个文件

/*************************************************** File name: get_file_size.h* Author: HAN Wei* Author's blog: http://blog.csdn.net/henter/* Date: Oct 31th, 2013* Description: declare function GetFileSize() which   can be used on platform Windows or Linux**************************************************/#ifndef GET_SMALL_FILE_SIZE_H  #define GET_SMALL_FILE_SIZE_H#ifdef  __cplusplusextern "C" {#endif/***************************************************函数名称:GetFileSize*功能: 获取文件的大小,结果以字节为单位*参数:     file_name[in]        文件名    file_byte_size[in]   文件大小*返回值:    0   成功    -1  失败*备注:1. 如果运行在Windows操作系统上,声明的函数只能在 Visual    Studio 2005 及更高版本上编译。2. 该函数对实际文件大小没有限制**************************************************/int GetFileSize(char *file_name, long long *file_byte_size);#ifdef  __cplusplus}#endif#endif /* end of GET_SMALL_FILE_SIZE_H */


第 2 个文件

/*************************************************** File name: get_file_size.c* Author: HAN Wei* Author's blog: http://blog.csdn.net/henter/* Date: Oct 31th, 2013* Description: implement function GetFileSize()**************************************************/#include "get_file_size.h"#include <stdio.h>#if defined(__linux__) || defined(__unix__)  #define _FILE_OFFSET_BITS 64#endifint GetFileSize(char *file_name, long long *file_byte_size){   FILE * fp;  if (!(fp=fopen(file_name, "rb")))  {#ifdef _DEBUG    printf("Open file %s failed!\n", file_name);#endif    return (-1);  }#if defined(_WIN32) || defined(_WIN64)  #if _MSC_VER >= 1400/***********************/  if ( _fseeki64(fp, (long long)(0), SEEK_END) )  {#ifdef _DEBUG    printf("fseek() function failed!\n");#endif    fclose(fp);    return (-1);  }  *file_byte_size=_ftelli64(fp);  #else    #error Visual Studio version is less than 8.0(VS 2005) !  #endif/***********************/#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)的字节长度,经测试发现都能够正确得到结果。

/*************************************************** File name: sample.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 <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");#endifreturn 1;  }  elseprintf("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。

原创粉丝点击