c程序设计语言_习题8-4_重新实现c语言的库函数fseek(FILE*fp,longoffset,intorigin)
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 = ,
_WRITE = ,
_UNBUF = ,
_EOF = ,
_ERR =
};
--------------------代码实现----------------------------
The standard library function
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) == && 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 = ;
f->ptr = f->base;
}
}
return (lseek(f->fd, offset, whence) < );
}
...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-4 I 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 2 int fseek(FILE *f, long offset, int whence)
{
int result; if ((f->flag & _UNBUF) == && 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 >= && offset <= f->cnt) {
/* easy shortcut */
f->cnt -= offset;
f->ptr += offset;
f->flags &= ~_EOF; /* see below */
return ;
} else
offset -= f->cnt;
}
f->cnt = ;
f->ptr = f->base;
}
}
result = (lseek(f->fd, offset, whence) < );
if (result == )
f->flags &= ~_EOF; /* if successful, clear EOF flag */
return result;
}
c程序设计语言_习题8-4_重新实现c语言的库函数fseek(FILE*fp,longoffset,intorigin)的更多相关文章
- c程序设计语言_习题1-16_自己编写getline()函数,接收整行字符串,并完整输出
Revise the main routine of the longest-line program so it will correctly print the length of arbitra ...
- c程序设计语言_习题7-6_对比两个输入文本文件_输出它们不同的第一行_并且要记录行号
Write a program to compare two files, printing the first line where they differ. Here's Rick's solut ...
- c程序设计语言_习题8-6_利用malloc()函数,重新实现c语言的库函数calloc()
The standard library function calloc(n,size) returns a pointer to n objects of size size , with the ...
- c程序设计语言_习题1-19_编写函数reverse(s)将字符串s中字符顺序颠倒过来。
Write a function reverse(s) that reverses the character string s . Use it to write a program that re ...
- c程序设计语言_习题1-18_删除输入流中每一行末尾的空格和制表符,并删除完全是空格的行
Write a program to remove all trailing blanks and tabs from each line of input, and to delete entire ...
- c程序设计语言_习题1-13_统计输入中单词的长度,并且根据不同长度出现的次数绘制相应的直方图
Write a program to print a histogram of the lengths of words in its input. It is easy to draw the hi ...
- c程序设计语言_习题1-11_学习单元测试,自己生成测试输入文件
How would you test the word count program? What kinds of input are most likely to uncover bugs if th ...
- c程序设计语言_习题1-9_将输入流复制到输出流,并将多个空格过滤成一个空格
Write a program to copy its input to its output, replacing each string of one or more blanks by a si ...
- 3.1 C语言_实现AVL平衡二叉树
[序] 上节我们实现了数据结构中最简单的Vector,那么来到第三章,我们需要实现一个Set set的特点是 内部有序且有唯一元素值:同时各种操作的期望操作时间复杂度在O(n·logn): 那么标准的 ...
随机推荐
- Xml通用操作类
using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Xml ...
- JSON字符串转换为JSON对象
一.JSON字符串转换为JSON对象 A:eval函数 eval函数可以直接将本质符合或者近似符合JSON格式的字符串转换为JSON对象,使用方式如: eval('(' + str + ')'); / ...
- live 写博
1 2 3 4 5 1 2 3 4 5 ...
- hdu 1568 Fibonacci 数学公式
Fibonacci Problem Description 2007年到来了.经过2006年一年的修炼,数学神童zouyu终于把0到的Fibonacci数列(f[0]=0,f[1]=1;f[i] = ...
- c语言的一些基础知识
c语言作为经典语言,这里不再多说了.咱从基础一起探讨吧! 一. 定义一个整型,如果作为局部变量,没有初始化的情况下,它是一个随机的值的,一般情况下输出会是0,但这个0是作为垃圾值的;而如果作为全局变量 ...
- 【web安全】第六弹:手工SQL注入详解
前一段时间,在对SQL注入有了新的理解之后,写了这篇文章.本来准备投稿,因为内容过于基础被打回来了,想想屯着也没意思,发出来发出来~~本来有好多图的,但是博客园发图很麻烦,word文档的链接会贴在文章 ...
- CODEVS 3000公路修建问题
题目描述 Description OI island是一个非常漂亮的岛屿,自开发以来,到这儿来旅游的人很多.然而,由于该岛屿刚刚开发不久,所以那里的交通情况还是很糟糕.所以,OIER Associat ...
- 利用 runtime,解决多次点击相同 button,导致重复跳转的问题-b
当app有点卡的时候,多次点击相同的button,经常出现,跳转了N次相同的界面(比如闲鱼) 解决办法 用运行时和分类,替换 UIControl 响应事件,根据响应的间隔时间来判断是否执行事件. 详细 ...
- 设置BootStrap导航条的高度
只要加上这段css就可以覆盖Bootstrap.css的代码,定制符合自己的样式 .navbar { min-height: 40px; } .nav > li > a { padding ...
- PHP漏洞全解(五)-SQL注入攻击
本文主要介绍针对PHP网站的SQL注入攻击.所谓的SQL注入攻击,即一部分程序员在编写代码的时候,没有对用户输入数据的合法性进行判断,使应用程序存在安全隐患.用户可以提交一段数据库查询代码,根据程序返 ...