server-api-fseek

原型

移动文件流的读写位置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// stream:已打开的文件的流指针
// offset:根据参数whence来移动读写位置的位移数
// whence:位置定位类型
// SEEK_SET 从距文件开头offset位移量为新的读写位置。
// SEEK_CUR 以目前的读写位置往后增加offset个位移量。
// SEEK_END 将读写位置指向文件尾后再增加offset个位移量。
// 当whence值为SEEK_CUR 或SEEK_END时,参数offset允许负值的出现。
//
// return:
// 当调用成功时则返回0,若有错误则返回-1,errno会存放错误代码。
//
// 附加说明:
// fseek()不像lseek()会返回读写位置,因此必须使用ftell()来取得目前读写的位置。
int fseek(FILE * stream, long offset, int whence);

使用参考

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>

int main()
{
FILE * stream;
long offset;
fpos_t pos;
stream = fopen("test", "r");

printf("offset before fseek %d\n", ftell(stream));
fseek(stream, 5, SEEK_SET);
printf("offset after fseek %d\n", ftell(stream));
}