详情请看 http://www.cplusplus.com/reference/cstdio/fseek/
(1) fseek()
定义: int fseek ( FILE * stream, long int offset, int origin );
功能:重新设置stream的位置到offset处基于文件头/文件尾/当前位置
参数:stream: 只想一个文件对象的指针.
offset: 二进制文件: Number of bytes to offset from origin. 文本文件: Either zero, or a value returned byftell.
origin: 偏移基于的位置.
It is specified by one of the following constants defined in<cstdio> exclusively to be used as arguments for this function:
Constant | Reference position |
---|---|
SEEK_SET | 文件头 |
SEEK_CUR | 当前位置 |
SEEK_END | 文件 |
Library implementations are allowed to not meaningfully support SEEK_END (therefore, code using it has no real standard portability).
返回值: 成功返回0 失败返回非0
/* fseek example */
#include <stdio.h>
int main ()
{
FILE * pFile;
pFile = fopen ( "example.txt" , "wb" );
fputs ( "This is an apple." , pFile );
fseek ( pFile , 9 , SEEK_SET );
fputs ( " sam" , pFile );
fclose ( pFile );
return 0;
}
(