当前位置:主页 > 软件编程 > C代码 >

c语言获取文件大小的示例

时间:2020-10-15 23:15:33 | 栏目:C代码 | 点击:

1.fseek

函数原型:

复制代码 代码如下:

int fseek ( FILE * stream, long int offset, int origin );

参数说明:stream,文件流指针;offest,偏移量;orgin,原(始位置。其中orgin的可选值有SEEK_SET(文件开始)、SEEK_CUR(文件指针当前位置)、SEEK_END(文件结尾)。

函数说明:对于二进制模式打开的流,新的流位置是origin + offset。

2.ftell

函数原型:long int ftell ( FILE * stream );

函数说明:返回流的位置。对于二进制流返回值为距离文件开始位置的字节数。

获取文件大小C程序(file.cpp):

复制代码 代码如下:

#include <stdio.h>

int main ()
{
      FILE * pFile;
      long size;

      pFile = fopen ("file.cpp","rb");
      if (pFile==NULL)
            perror ("Error opening file");
      else
      {
            fseek (pFile, 0, SEEK_END);   ///将文件指针移动文件结尾
            size=ftell (pFile); ///求出当前文件指针距离文件开始的字节数
            fclose (pFile);
            printf ("Size of file.cpp: %ld bytes.\n",size);
      }
      return 0;
}

您可能感兴趣的文章:

相关文章