牛骨文教育服务平台(让学习变的简单)
博文笔记

linux c 语言之--fseek(),fseeko(),fseeko64(),ftell(),ftello()讲解

创建时间:2017-10-10 投稿人: 浏览次数:615

转载:http://blog.csdn.net/lemoncyb/article/details/16841317

函数定义:

int fseek(FILE *stream, long offset, int fromwhere);

int fseeko(FILE *stream, off_t offset, int fromwhere);

int fseeko64(FILE *stream, off64_t offset, int fromwhere);

参数:

stream:文件指针

fromwhere:偏移起始位置

offset:偏移量

功能:

函数设置文件指针stream的位置。如果执行成功,stream将指向以fromwhere(偏移起始位置:文件头0(SEEK_SET),当前位置1(SEEK_CUR),文件尾2(SEEK_END))为基准,偏移offset(指针偏移量)个字节的位置。如果执行失败(比如offset超过文件自身大小),则不改变stream指向的位置。

说明:这几个函数唯一的不同是offset的数据类型不同,相应的能够处理的偏移量的范围也就有大有小。

返回值:

成功,返回0,失败返回-1,并设置errno的值,可以用perror()函数输出错误。
fseek position the file(文件) position(位置) pointer(指针) for the file referenced by stream to the byte location calculated by offset.

#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);

相关参数

对于二进制文件:whence有三个取值SEEK_SET、SEEK_CUR、SEEK_END
对于文本文件:whence必须为SEEK_SET,offset只能为两个值,0—代表rewind到文件开头,或者使用ftell的返回值
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。