fseek、ftell和rewind函数详解

来源:互联网 发布:mac numbers 使用技巧 编辑:程序博客网 时间:2024/04/30 14:48

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

重定位流(数据流/文件)上的文件内部位置指针
注意:不是定位文件指针,文件指针指向文件/流。位置指针指向文件内部的字节位置,随着文件的读取会移动,文件指针如果不重新赋值将不会改变指向别的文件。
函数设置文件指针stream的位置。如果执行成功,stream将指向以fromwhere(偏移起始位置:文件头0(SEEK_SET),当前位置1(SEEK_CUR),文件尾2(SEEK_END))为基准,偏移offset(指针偏移量)个字节的位置。如果执行失败(比如offset超过文件自身大小),则不改变stream指向的位置。
fseek函数和lseek函数类似,但lseek返回的是一个off_t数值,而fseek返回的是一个整型。成功,返回0,失败返回-1,并设置errno的值,可以用perror()函数输出错误。
fseek position the file(文件) position(位置) pointer(指针) for the file referenced by stream to the byte location calculated by offset.
程序例子:
#include <stdio.h>long filesize(FILE *stream);int main(void){FILE *stream;stream = fopen("MYFILE.TXT", "w+");fprintf(stream, "This is a test");printf("Filesize of MYFILE.TXT is %ld bytes\n", filesize(stream));fclose(stream);return 0;}long filesize(FILE *stream){long curpos, length;curpos = ftell(stream);fseek(stream, 0L, SEEK_END);length = ftell(stream);fseek(stream, curpos, SEEK_SET);return length;}
第一个参数stream为文件指针
第二个参数offset为偏移量,正数表示正向偏移,负数表示负向偏移
第三个参数origin设定从文件的哪里开始偏移,可能取值为:SEEK_CUR、 SEEK_END 或 SEEK_SET
SEEK_SET: 文件开头
SEEK_CUR: 当前位置
SEEK_END: 文件结尾
其中SEEK_SET,SEEK_CUR和SEEK_END依次为0,1和2.
简言之:
fseek(fp,100L,0);把文件内部指针移动到离文件开头100字节处;
fseek(fp,100L,1);把文件内部指针移动到离文件当前位置100字节处;
fseek(fp,-100L,2);把文件内部指针退回到离文件结尾100字节处。
使用实例:
#include <stdio.h>#define N 5typedef struct student {long sno;char name[10];float score[3];} STU;void fun(char *filename, STU n){FILE *fp;fp = fopen(filename, "rb+");fseek(fp, -1L*sizeof(STU),SEEK_END);fwrite(&n, sizeof(STU), 1, fp);fclose(fp);}void main()/*修改覆盖最后一个学生数据*/{STU t[N]={ {10001,"MaChao", 91, 92, 77}, {10002,"CaoKai", 75, 60, 88},{10003,"LiSi", 85, 70, 78}, {10004,"FangFang", 90, 82, 87},{10005,"ZhangSan", 95, 80, 88}};STU n={10006,"ZhaoSi", 55, 70, 68}, ss[N];int i,j; FILE *fp;fp = fopen("student.dat", "wb");fwrite(t, sizeof(STU), N, fp);fclose(fp);fp = fopen("student.dat", "rb");fread(ss, sizeof(STU), N, fp);fclose(fp);printf("\nThe original data :\n\n");for (j=0; j<N; j++){printf("\nNo: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name);for (i=0; i<3; i++) printf("%6.2f ", ss[j].score[i]);printf("\n");}fun("student.dat", n);printf("\nThe data after modifing :\n\n");fp = fopen("student.dat", "rb");fread(ss, sizeof(STU), N, fp);fclose(fp);for (j=0; j<N; j++){printf("\nNo: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name);for (i=0; i<3; i++) printf("%6.2f ", ss[j].score[i]);printf("\n");}}
注意事项:
fseek函数的文件指针,应该为已经打开的文件。如果没有打开的文件,那么将会出现错误。 fseek函数也可以这样理解,相当于在文件当中定位。这样在读取规律性存储文件时可以利用其OFFSET偏移量读取文件上任意的内容。
fseek函数一般用于二进制文件,也可以用于文本文件。用于文本文件操作时,需特别注意回车换行的情况:因为在一般浏览工具如UltraEdit中,回车换行视为两个字符0x0D和0x0A,但真实的文件读写和定位时却按照一个字符0x0A进行处理,因此碰到此类问题时,可以考虑将文件整个读入内存,然后在内存中手工插入0x0D的方法,这样可以达到较好的处理效果。
 long ftell(FILE *stream);
函数 ftell() 用于得到文件位置指针当前位置相对于文件首的偏移字节数。在随机方式存取文件时,由于文件位置频繁的前后移动,程序不容易确定文件的当前位置。使用fseek函数后再调用函数ftell()就能非常容易地确定文件的当前位置。
ftell(fp);利用函数 ftell() 也能方便地知道一个文件的长。如以下语句序列: fseek(fp, 0L,SEEK_END); len =ftell(fp)+1; 首先将文件的当前位置移到文件的末尾,然后调用函数ftell()获得当前位置相对于文件首的位移,该位移值等于文件所含字节数。
举例1:
#include <stdio.h>int main(void){FILE *stream;stream = fopen("MYFILE.TXT", "w+");fprintf(stream, "This is a test");printf("The file pointer is at byte \%ld\n", ftell(stream));fclose(stream);return 0;}
举例2:读取文本文件中的内容
#include <stdio.h>#include <stdlib.h>int main(){FILE *fp;int flen;char *p;/* 以只读方式打开文件 */if((fp = fopen ("1.txt","r"))==NULL){printf("\nfile open error\n");exit(0);}fseek(fp,0L,SEEK_END); /* 定位到文件末尾 */flen=ftell(fp); /* 得到文件大小 */p=(char *)malloc(flen+1); /* 根据文件大小动态分配内存空间 */if(p==NULL){fclose(fp);return 0;}fseek(fp,0L,SEEK_SET); /* 定位到文件开头 */fread(p,flen,1,fp); /* 一次性读取全部文件内容 */p[flen]=0; /* 字符串结束标志 */printf("%s",p);fclose(fp);free(p);return 0;}程序改进#include <stdio.h>main(){FILE *myf;long f1;myf=fopen("1.txt","rb");fseek(myf,0,SEEK_END);f1=ftell(myf);fclose(myf);printf(“%d\n”,f1);}

void rewind(FILE *stream);
         将文件内部的位置指针重新指向一个流(数据流/文件)的开头。
注意:不是文件指针而是文件内部的位置指针,随着对文件的读写文件的位置指针(指向当前读写字节)向后移动。而文件指针是指向整个文件,如果不重新赋值文件指针不会改变。
英文解释: A statement such as
rewind( cfptr );
causes a program's file position--which indicates the number of the next byte in the file to be read or written-- to be repositioned to the beginnning of the file pointed to by cfptr.
程序例:
#include <stdio.h>#include <dir.h>int main(void){FILE *fp;char fname[10] = "TXXXXXX", *newname, first;newname = mktemp(fname);fp = fopen(newname,"w+");if(NULL==fp)return 1;fprintf(fp,"abcdefghijklmnopqrstuvwxyz");rewind(fp);fscanf(fp,"%c",&first);printf("The first character is: %c\n",first);fclose(fp);remove(newname);return 0;}










0 0
原创粉丝点击