ftell函数

函数名

: ftell

头文件

: <stdio.h>

功 能

: 返回 当前文件位置,也就是说返回FILE 指针当前位置。

函数原型

: long ftell(FILE *stream);

函数功能

:函数 ftell() 用于得到文件位置指针当前位置相对于文件首的偏移字节数。在随机方式存取文件时,由于文件位置频繁的前后移动,程序不容易确定文件的当前位置。使用fseek函数后再调用函数ftell()就能非常容易地确定文件的当前位置。

函数名

:fseek
用来设定文件的当前读写位置.

函数原型

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


函数功能

把fp的文件读写位置指针移到指定的位置.


        fseek(fp,20,SEEK_SET); 意思是把fp文件读写位置指针从文件开始后移20个字节.
fseek(fp,20,SEEK_SET); 意思是把fp文件读写位置指针从文件开始后移20个字节.

fseek函数与ftell函数综合应用:


分析:可以用fseek函数把位置指针移到文件尾,再用ftell函数获得这时位置指针距文件头的字节数,这个字节数就是文件的长度.
#include <stdio.h>

main()

{

   FILE *fp;

   char filename[80];

   long length;

   printf("输入文件名:");

   gets(filename);

   //以二进制读文件方式打开文件

   fp=fopen(filename,"rb");

   if(fp==NULL)

      printf("file not found!\n");

   else

      {

         //把文件的位置指针移到文件尾

          fseek(fp,OL,SEEK_END);

         //获取文件长度;

          length=ftell(fp);

          printf("该文件的长度为%1d字节\n",length);

          fclose(fp);

      }

}




你可能感兴趣的:(ftell函数)