/*
名称:C文件操作的定位
说明:rewind(fp):将文件读/写指针移到文件的开始位置
fseek(fp,offset,whence):将文件读/写指针从whence标识的位置移动offset个字节
其中offset可以去负值代表向后移动,在此函数中,whence取0代表文件开头,2代表
当前位置,3代表文件末尾。此函数多用于二进制文件中,文本文件可能字符转换的
过程中出现问题。
*/
#include<stdio.h>
int main()
{
char c;
FILE *fp;
long pos; //指明文件指针当前的位置
fp = fopen("text.dat","wa+");
if(fp == NULL)
{
printf("open the file wrongly!");
}
else
{
//fprintf(fp,"%s--%d\n","test",20);
fputs("123456789abcdefghij",fp);
}
pos = ftell(fp); //获得文件的当前指针
printf("the file pointer's pos is :%ld\n",pos);
rewind(fp); //使指针移动到开始位置
printf("the first character is %c.\n",fgetc(fp));
fseek(fp,5L,0);
printf("the fifth character is %c.\n",fgetc(fp));
return 0;
}