ftell/ftello、fseek/fseeko、fsetpos/fgetpos、rewind

标准IO流中的文件定位函数详解
这篇博客详细讲解了在标准IO流中用于文件位置操作的函数,包括ftell/ftello、fseek/fseeko、fsetpos/fgetpos和rewind。ftell和ftello提供文件当前位置的数值,fseeko和fsetpos用于设置和获取文件位置,而rewind则将文件指针重置到开头。对于文本和二进制文件,这些函数有不同的行为和限制。fpos_t对象可能在非UNIX系统中较为复杂,使得fgetpos和fsetpos成为便携式重新定位文本流的重要方式。

讲解了在标准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);

fseekftelloffset都是long型的数。

  • 对于二进制文件:whence有三个取值SEEK_SETSEEK_CURSEEK_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

相对于fseekftell,将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

回顾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);

fgetposfile‘s position indicator的当前值存放到pos指向的object中去。
fsetpos相反

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猎羽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值