fseek
功能:将文件偏移量修改到指定位置; 原型: #include <stdio.h> int fseek(FILE *stream, long offset, int whence); 参数: FILE *stream:指定要修改偏移量的文件对应的流指针; long offset:距离whence的偏移量:若想要往后偏移,则填正数;若想往前偏移,则填负数; int whence: SEEK_SET, 文件开头位置 SEEK_CUR, 文件当前位置 SEEK_END, 文件结尾位置 返回值: 成功,返回0; 失败,返回-1, 更新errno; 将偏移量修改到文件开头 void rewind(FILE *stream); ===》 fseek(fp, 0, SEEK_SET);
ftell
功能:获取文件当前位置距离文件开头的偏移量; 原型: long ftell(FILE *stream); 返回值: 文件当前位置距离文件开头的偏移量; fseek(fp, 0, SEEK_END); //将偏移量修改到结尾 long size = ftell(fp); printf("size = %ld\n", size);
#include<stdio.h>
int main(int argc, const char *argv[])
{
FILE *fp=fopen("./02_std.c","r");
if(NULL==fp)
{
perror("fopen");
return -1;
}
int a=fseek(fp,0,SEEK_END);
int b=ftell(fp);
printf("a=%d\n",a);
printf("b=%d\n",b);
fclose(fp);
return 0;
}