ftell/ftello、fseek/fseeko、fsetpos/fgetpos、rewind
讲解了在标准IO流中position(位置)相关函数
#include <stdio.h>
int fseek(FILE *stream, long offset, int whence);
//Returns: current file position indicator(指示器) if OK, -1L on error
long ftell(FILE *stream);
//Returns:0 if OK, nonzero on error
void rewind(FILE *stream);- 1
- 2
- 3
- 4
- 5
- 6
- 7
fseek和ftell的offset都是long型的数。
- 对于二进制文件:
whence有三个取值SEEK_SET、SEEK_CUR、SEEK_END - 对于文本文件:
whence必须为SEEK_SET,offset只能为两个值,0—代表rewind到文件开头,或者使用ftell的返回值
rewindfunction sets the file position indicator for the stream pointed to by stream to the beginning of the file. It is equivalent to:(void) fseek(stream, 0L, SEEK_SET)
fseeko, ftello - seek to or report file position
相对于fseek和ftell,将offset的类型从long换成off_t
#include <stdio.h>
int fseeko(FILE *stream, off_t offset, int whence);
//Returns: current file position indicator if OK, (off_t) -1 on error
off_t ftello(FILE *stream);
//Returns:0 if OK, nonzero on error- 1
- 2
- 3
- 4
- 5
- 6
回顾3.6节关于off_t的讨论,off_t也可以大于32bits
fgetpos、fsetpos
The fgetpos() and fsetpos() functions are alternate interfaces equivalent to ftell() and fseek() (with whence set to SEEK_SET), setting and storing the current value of the file offset into or from the object referenced by pos.
On some non-UNIX systems, an fpos_t object may be a complex object and these routines may be the only way to portably reposition a text stream.
#include <stdio.h>
int fgetpos(FILE *stream, fpos_t *pos);
int fsetpos(FILE *stream, const fpos_t *pos);- 1
- 2
- 3
fgetpos将file‘s position indicator的当前值存放到pos指向的object中去。 fsetpos相反
本文详细介绍了标准IO流中的位置操作函数,包括fseek/fseeko用于定位文件位置,ftell/ftello获取当前位置,rewind重置位置到文件开始,以及fgetpos/fsetpos保存和设置文件位置的方法。
759

被折叠的 条评论
为什么被折叠?



