jasmine214--love

只有当你的内心总是充满快乐、美好的愿望和宁静时,你才能拥有强壮的体魄和明朗、快乐或者宁静的面容。
posts - 731, comments - 60, trackbacks - 0, articles - 0

C--fseek函数

Posted on 2011-03-08 16:26 幻海蓝梦 阅读(1741) 评论(1)  编辑  收藏 所属分类: C语言学习

fseek函数是用来设定文件的当前读写位置:

函数原型:int fseek(FILE *fp,long offset,int origin);
函数功能:把fp的文件读写位置指针移到指定的位置.

fseek(fp,20,SEEK_SET);

//意思是把fp文件读写位置指针从文件开始后移20个字节.

ftell函数是用来获取文件的当前读写位置;
函数原型: long ftell(FILE *fp)
函数功能:得到流式文件的当前读写位置,其返回值是当前读写位置偏离文件头部的字节数.

ban=ftell(fp);

//是获取fp指定的文件的当前读写位置,并将其值传给变量ban.

fseek函数与ftell函数综合应用:
分析:可以用fseek函数把位置指针移到文件尾,再用ftell函数获得这时位置指针距文件头的字节数,这个字节数就是文件的长度.

  1. #include <stdio.h>  
  2. main()  
  3. {  
  4.    FILE *fp;  
  5.    char filename[80];  
  6.    long length;  
  7.    printf("Input the file name:");  
  8.    gets(filename);  
  9.    fp=fopen(filename,"rb");  
  10.    if(fp==NULL)  
  11.        printf("file not found!\n");  
  12.    else  
  13.    {  
  14.        fseek(fp,OL,SEEK_END);  
  15.        length=ftell(fp);  
  16.        printf("the file length %1d bytes\n",length);  
  17.        fclose(fp);  
  18.    }  
  19. }  
原文:http://blog.csdn.net/swliao/archive/2009/09/04/4518012.aspx

Feedback

# re: C--fseek函数[未登录]  回复  更多评论   

2011-03-25 11:32 by byron
謝謝你啊,很有用

只有注册用户登录后才能发表评论。


网站导航: