设为首页 加入收藏

TOP

用C语言获取任意文件的长度(七)
2013-11-20 14:18:21 来源: 作者: 【 】 浏览:970
Tags:语言 获取 任意 文件 长度

 

  #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.

      

首页 上一页 4 5 6 7 8 9 10 下一页 尾页 7/12/12
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C语言排序算法 下一篇C语言实现函数间jump的方法

评论

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