c程序设计语言_习题8-4_重新实现c语言的库函数fseek(FILE*fp,longoffset,intorigin)

news/2024/7/5 2:02:42

  fseek库函数

#include <stdio.h>
int fseek(FILE  *stream, long  int  offset, int  origin);
返回:成功为0,出错为非0 

对流stream相关的文件定位,随后的读写操作将从新位置开始。

对于二进制文件,此位置被定位在由origin开始的offset个字符处。origin的值可能为SEEK_SET(文件开始处)、SEEK_CUR(当前位置)或SEEK_END(文件结束处)。

对于文本流,offset心须为0,或者是由函数ftell()返回的值(此时origin的值必须是SEEK_SET)(这里关于与ftell函数的交互,不是很理解。)。

 

ftell库函数

 

#include <stdio.h>
long int ftell(FILE *stream);

返回与流stream相关的文件的当前位置。出错时返回-1L。

 

fflush库函数

 
#include <stdio.h>
int fflush(FILE *stream);
返回:成功为0,失败返回EOF

对输出流(写打开),fflush()用于将已写到缓冲区但尚未写出的全部数据都写到文件中;对输入流,其结果未定义。如果写过程中发生错误则返回EOF,正常则返回0。

fflush(NULL)用于刷新所有的输出流。

程序正常结束或缓冲区满时,缓冲区自动清仓。

 

 lseek库函数

头文件:#include <sys/types.h>    #include <unistd.h>定义函数:off_t lseek(int fildes, off_t offset, int whence);

 lseek函数不是ANSI C标准库函数,只是满足POSIX的UNIX下的函数。

函数说明:
每一个已打开的文件都有一个读写位置, 当打开文件时通常其读写位置是指向文件开头, 若是以附加的方式打开文件(如O_APPEND), 则读写位置会指向文件尾. 当read()或write()时, 读写位置会随之增加,lseek()便是用来控制该文件的读写位置. 参数fildes 为已打开的文件描述词, 参数offset 为根据参数whence来移动读写位置的位移数.

 

涉及到的枚举变量

 

复制代码
 enum _flags{_READ     = 01,_WRITE     = 02,_UNBUF     = 04,_EOF    = 010,_ERR    = 020};
复制代码

 

--------------------代码实现----------------------------

 

The standard library function 

1
int fseek(FILE*fp,long offset,int origin)  

is identical to lseek except that fp is a file pointer instead of a file descriptor and the return value is an int status, not a position. Write fseek . Make sure that your fseek coordinates properly with the buffering done for the other functions of the library. 

Here's Gregory's first solution: 

 
复制代码
/* Gregory Pietsch -- My category 0 solution to 8-4 */int fseek(FILE *f, long offset, int whence)
{if ((f->flag & _UNBUF) == 0 && base != NULL) 
{
/* deal with buffering */if (f->flag & _WRITE)
{
/* writing, so flush buffer */fflush(f); /* from 8-3 */}
     else if (f->flag & _READ)
{
/* reading, so trash buffer */f->cnt = 0;f->ptr = f->base;}}return (lseek(f->fd, offset, whence) < 0); }
复制代码

 



...and here's his second, which is considerably more comprehensive:

 
复制代码
/*[The following solution is in the zip file as krx80401.c - RJH (ed.) ]EXERCISE 8-4I thought I'd improve 8-4 too.  I'm trying my best to get this as close
to ISO C as possible given the restrictions that I'm under.  (A real
implementation would have fsetpos() borrow some of the same code.)*//* Gregory Pietsch -- My category 0 solution to 8-4 */#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2int fseek(FILE *f, long offset, int whence)
{int result;if ((f->flag & _UNBUF) == 0 && base != NULL) {/* deal with buffering */if (f->flag & _WRITE) {/* writing, so flush buffer */if (fflush(f))return EOF;  /* from 8-3 */} else if (f->flag & _READ) {/* reading, so trash buffer --* but I have to do some housekeeping first*/if (whence == SEEK_CUR) {/* fix offset so that it's from the last * character the user read (not the last* character that was actually read)*/if (offset >= 0 && offset <= f->cnt) {/* easy shortcut */f->cnt -= offset;f->ptr += offset;f->flags &= ~_EOF; /* see below */return 0;} elseoffset -= f->cnt;}f->cnt = 0;f->ptr = f->base;}}result = (lseek(f->fd, offset, whence) < 0);if (result == 0)f->flags &= ~_EOF; /* if successful, clear EOF flag */return result;
}
复制代码

 

本文转自二郎三郎博客园博客,原文链接:http://www.cnblogs.com/haore147/p/3648307.html,如需转载请自行联系原作者

http://lihuaxi.xjx100.cn/news/241460.html

相关文章

centos 7.2 yum mysql_20191209_Centos7.2使用yum安装mysql

1. 下载mysql的rpm包[rootizwz91qnvovd6suufon1ccz ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm2. 安装rpm包[rootizwz91qnvovd6suufon1ccz ~]# yum localinstall -y mysql57-community-release-el7-7.noarch.rpm3. 安装mysql 5.7[rootizwz…

python基本数据类型之序列类型和映射类型

序列类型&#xff1a;字符串/元组/列表 映射类型&#xff1a;字典 更正&#xff1a;&#xff1a;三引号也可以用来表示字符串&#xff0c;并且有额外用途&#xff1a;①搞定多行字符串 ②内用单引号和双引号 列表可以根据内容得到索引 有多个相同内容时根据第一个得到下标

spring读取配置文件的几种方式

场景 假如有以下属性文件dev.properties, 需要注入下面的tag tag123 通过PropertyPlaceholderConfigurer <bean class"org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name"location" value"dev.prop…

mysql5.6 thread pool_mysql5.6 thread pool

从percona 的压测来看&#xff0c;确实很牛笔啊。提升很大。http://www.mysqlperformanceblog.com/2014/01/29/percona-server-thread-pool-improvements/当然&#xff0c;他指出目前mysql5.6 有2套thread pool &#xff0c;一套是mysql企业版里面的&#xff0c;另外一套是mari…

【 Gym - 101138J 】Valentina and the Gift Tree(树链剖分)

BUPT2017 wintertraining(15) 4 DGym - 101138J数据 题意 n个节点的一棵树&#xff0c;每个节点的权值为g&#xff0c;q个询问&#xff0c;树上的节点U-V&#xff0c;求U到V的路径的最大子段和。 题解 先考虑这么一个问题&#xff1a;求区间[L,R]的最大子段和。 q个询问&#x…

php mysql ajax日历记事本_php+mysql+jquery日历签到

在网站开发过程中我们会经常用到签到功能来奖励用户积分&#xff0c;或者做一些其他活动。这次项目开发过程中做了日历签到&#xff0c;因为没有经验所有走了很多弯路&#xff0c;再次记录过程和步骤。1.日历签到样式&#xff1a;2.本次签到只记录本月签到数&#xff0c;想要查…

云计算重构渠道商的价值基础,推动渠道商向服务商转型

第九届中国软件渠道大会暨2016中国软件生态大会将于3月29日在武汉开启首站。会议召开在即&#xff0c;中国软件网联合海比研究推出“中国软件渠道商系列访谈”&#xff0c;对转型中的软件渠道商面临的挑战与机遇、心得体会&#xff0c;以及面对来势汹汹生态力量的感受展开探讨。…