`
kanwoerzi
  • 浏览: 1645352 次
文章分类
社区版块
存档分类
最新评论

unix环境高级编程-5.6-读和写流

 
阅读更多

一旦打开了一个流,则可在三种不同的类型的非格式化I/O,中进行选择,对其进行读、写操作:

(1)每次一个字符的I/O.一次读或写一个字符。则标准的io函数处理所有缓冲。

(2)每次一行的IO,如果想要一次读或写遗憾,则使用fgets和fputs函数。每行都以一个换行符终止。当调用fgets,要说明最大处理行长。

(3)直接IO,fread和fwrite函数支持这种类型的IO,每次io操作读或者写某种数量的对象,而每个对象具有指定的长度。

1.输入函数

以下三个函数可用于一次读一个字符


返回值,:若成功则返回下一个字符,若已到达文件结尾或出错则返回EOF.

查看GNU C

int fgetc (FILE *stream) [Function]
This function reads the next character as an unsigned char from the stream stream
and returns its value, converted to an int. If an end-of-file condition or read error
occurs, EOF is returned instead.

int getc (FILE *stream) [Function]
This is just like fgetc, except that it is permissible (and typical) for it to be implemented
as a macro that evaluates the stream argument more than once. getc is often
highly optimized, so it is usually the best function to use to read a single character.

int getchar (void) [Function]
The getchar function is equivalent to getc with stdin as the value of the stream
argument.

函数getchar等价于getc(stdin),前两个函数的区别是getc可被实现宏,而fgetc则不能实现为宏。这意味着:

(1)getc的参数不应当具有副作用的表达式。

(2) 因为fgetc一定是一个函数,所以可以得到其他地址。这就是允许将fgetc的地址作为一个参数传送个另一个函数。

(3)调用fgetc所需要的时间很可能长于调用getc,因为调用函数通常昂所需要的时间长于调用宏。

这三个函数在返回下一个字符时候,会将其unsigned char类型转换为int类型。说明为不带符号的理由是,如果最高位为1,也不会使返回值为负的。要求整型返回值的理由是,这样就可以返回所有可能的字符值再加上一个出错或已到达文件尾段的指示值。在<stdio.h>中的常量EOF被要求是一个负值。其值经常是-1.这就意味着不能将这三个函数的返回值存放在一个字符变量中,以后还要将这些函数的返回值与常量EOF相比较。

注意,不管是出错还是达到文件尾端,这三个函数都返回同样的值。为了区分这两种不同的情况,必须调用ferror或feof

上两个函数的返回值:若条件为真,返回值返回非0,(真),否则返回0(假);

int ferror (FILE *stream) [Function]
The ferror function returns nonzero if and only if the error indicator for the stream
stream is set, indicating that an error has occurred on a previous operation on the
stream.
This symbol is declared in ‘stdio.h’.

int feof (FILE *stream) [Function]
The feof function returns nonzero if and only if the end-of-file indicator for the
stream stream is set.
This symbol is declared in ‘stdio.h’.

void clearerr (FILE *stream) [Function]
This function clears the end-of-file and error indicators for the stream stream.
The file positioning functions (see Section 12.18 [File Positioning], page 278) also clear
the end-of-file indicator for the stream.

在大多数视线中,为每个流在FILE对象中维持了两个标志:

  • 出错标志。
  • 文件结束标志。

调用clearerr清除了这两个标志。

从流中读取数据以后,可以调用ungetc将字符在压送回流中。

int ungetc (int c, FILE *stream) [Function]
The ungetc function pushes back the character c onto the input stream stream. So
the next input from stream will read c before anything else.
If c is EOF, ungetc does nothing and just returns EOF. This lets you call ungetc with
the return value of getc without needing to check for an error from getc.
The character that you push back doesn’t have to be the same as the last character
that was actually read from the stream. In fact, it isn’t necessary to actually read
any characters from the stream before unreading them with ungetc! But that is a
strange way to write a program; usually ungetc is used only to unread a character
that was just read from the same stream. The GNU C library supports this even on
files opened in binary mode, but other systems might not.
The GNU C library only supports one character of pushback—in other words, it does
not work to call ungetc twice without doing input in between. Other systems might
let you push back multiple characters; then reading from the stream retrieves the
characters in the reverse order that they were pushed.
Pushing back characters doesn’t alter the file; only the internal buffering for the
stream is affected. If a file positioning function (such as fseek, fseeko or rewind;
see Section 12.18 [File Positioning], page 278) is called, any pending pushed-back
characters are discarded.
Unreading a character on a stream that is at end of file clears the end-of-file indicator
for the stream, because it makes the character of input available. After you read that
character, trying to read again will encounter end of file.

压送回到流中的字符以后又可以从流中读出来。但是读出的字符的顺序与压送的的顺序相反。应当了解,虽然ISO C,允许实现支持任何次数的回送。但是它要求实现提哦你哥哥一次只送回一个字符。。

回送的字符不一定是上一次独到的字符,不能回送EOF.但是当已经到达文件尾段的时候。人可以回送一个字符。下次读讲啊ing返回该字符,再次读则返回EOF。之所以这样做的原因是一次成功的ungetc调用会清除该流的文件结束标志。

当真再读一个输入流的时候,并进行某种形式的分字或分记号操作时。会经常用到回送字符操作。有时候需要先看看下一个字符,再在进行操作。然后就需要方便的姜刚查看的字符送回。一边下一次调用getc时候返回该字符。

如果标准I/O不提供回送能力,就需要将该字符存放到一个我们自己的变量中。并设置一个标志一遍判别在下一个需要一个字符的时候调用getc。

2输出函数

对于上面所诉的输入函数都一个对应的输出函数

int putc (int c, FILE *stream) [Function]
This is just like fputc, except that most systems implement it as a macro, making
it faster. One consequence is that it may evaluate the stream argument more than
once, which is an exception to the general rule for macros. putc is usually the best
function to use for writing a single character.

int fputc (int c, FILE *stream) [Function]
The fputc function converts the character c to type unsigned char, and writes it to
the stream stream. EOF is returned if a write error occurs; otherwise the character c
is returned.

int putchar (int c) [Function]
The putchar function is equivalent to putc with stdout as the value of the stream
argument.

更多内容欢迎访问:http://blog.csdn.net/wallwind

分享到:
评论

相关推荐

    UNIX环境高级编程

    5.6 读和写流 96 5.6.1 输入函数 96 5.6.2 输出函数 97 5.7 每次一行I/O 98 5.8 标准I/O的效率 99 5.9 二进制I/O 100 5.10 定位流 102 5.11 格式化I/O 103 5.11.1 格式化输出 103 5.11.2 格式化输入 103 5.12 实现...

    UNIX环境高级编程第二版

    5.6 读和写流 96 5.6.1 输入函数 96 5.6.2 输出函数 97 5.7 每次一行I/O 98 5.8 标准I/O的效率 99 5.9 二进制I/O 100 5.10 定位流 102 5.11 格式化I/O 103 5.11.1 格式化输出 103 5.11.2 格式化输入 103 5.12 实现...

    UNIX环境高级编程.pdf

    5.6 读和写流 96 5.6.1 输入函数 96 5.6.2 输出函数 97 5.7 每次一行I/O 98 5.8 标准I/O的效率 99 5.9 二进制I/O 100 5.10 定位流 102 5.11 格式化I/O 103 5.11.1 格式化输出 103 5.11.2 格式化输入 103 5.12 实现...

    UNIX环境高级编程(中文版+英文版+源代码)

    5.6 读和写流 96 5.6.1 输入函数 96 5.6.2 输出函数 97 5.7 每次一行I/O 98 5.8 标准I/O的效率 99 5.9 二进制I/O 100 5.10 定位流 102 5.11 格式化I/O 103 5.11.1 格式化输出 103 5.11.2 格式化输入 103 5.12 实现...

    UNIX环境高级编程(PDF)

    5.6 读和写流 96 5.6.1 输入函数 96 5.6.2 输出函数 97 5.7 每次一行I/O 98 5.8 标准I/O的效率 99 5.9 二进制I/O 100 5.10 定位流 102 5.11 格式化I/O 103 5.11.1 格式化输出 103 5.11.2 格式化输入 103 5.12 实现...

    UNIX环境高级编程_第2版.part2

    5.6 读和写流114 5.7 每次一行i/o 116 5.8 标准i/o的效率117 5.9 二进制i/o 119 5.10 定位流120 5.11 格式化i/o 121 5.12 实现细节125 5.13 临时文件127 5.14 标准i/o的替代软件130 5.15 小结130 习题130 ...

    UNIX环境高级编程中文版

    5.6 读和写流 96 5.6.1 输入函数 96 5.6.2 输出函数 97 5.7 每次一行I/O 98 5.8 标准I/O的效率 99 5.9 二进制I/O 100 5.10 定位流 102 5.11 格式化I/O 103 5.11.1 格式化输出 103 5.11.2 格式化输入 103 5.12 实现...

    Unix环境高级编程电子书

    5.6 读和写流 96 5.6.1 输入函数 96 5.6.2 输出函数 97 5.7 每次一行I/O 98 5.8 标准I/O的效率 99 5.9 二进制I/O 100 5.10 定位流 102 5.11 格式化I/O 103 5.11.1 格式化输出 103 5.11.2 格式化输入 103 5.12 实现...

    UNIX环境高级编程和源代码

    5.6 读和写流 96 5.6.1 输入函数 96 5.6.2 输出函数 97 5.7 每次一行I/O 98 5.8 标准I/O的效率 99 5.9 二进制I/O 100 5.10 定位流 102 5.11 格式化I/O 103 5.11.1 格式化输出 103 5.11.2 格式化输入 103 5.12 实现...

    UNIX环境高级编程 不扣分哦

    5.6 读和写流 96 5.6.1 输入函数 96 5.6.2 输出函数 97 5.7 每次一行I/O 98 5.8 标准I/O的效率 99 5.9 二进制I/O 100 5.10 定位流 102 5.11 格式化I/O 103 5.11.1 格式化输出 103 5.11.2 格式化输入 103 5.12 实现...

    UNIX环境高级编程_第二版中文

    5.6 读和写流  5.7 每次一行I/O  5.8 标准I/O的效率  5.9 二进制I/O  5.10 定位流  5.11 格式化I/O  5.12 实现细节  5.13 临时文件  5.14 标准I/O的替代软件  5.15 小结  习题  第6章 系统...

    中文第一版-UNIX环境高级编程

    5.6 读和写流 96 5.6.1 输入函数 96 5.6.2 输出函数 97 5.7 每次一行I/O 98 5.8 标准I/O的效率 99 5.9 二进制I/O 100 5.10 定位流 102 5.11 格式化I/O 103 5.11.1 格式化输出 103 5.11.2 格式化输入 103 5.12 实现...

    UNIX环境高级编程(第二版中文).pdf

    5.6 读和写流 96 5.6.1 输入函数 96 5.6.2 输出函数 97 5.7 每次一行I/O 98 5.8 标准I/O的效率 99 5.9 二进制I/O 100 5.10 定位流 102 5.11 格式化I/O 103 5.11.1 格式化输出 103 5.11.2 格式化输入 103 5.12 实现...

    UNIX环境高级编程(第二版中文)

    UNIX环境高级编程 第1章 UNIX基础知识 1 1.1 引言 1 1.2 UNIX体系结构 1 1.3 登录 1 1.4 文件和目录 3 1.5 输入和输出 6 1.6 程序和进程 8 1.7 出错处理 10 1.8 用户标识 12 1.9 信号 14 1.10 ...

    UNIX环境高级编程英文第三版+源码

    5.6 Reading and Writing a Stream 150 5.7 Line-at-a-Time I/O 152 5.8 Standard I/O Efficiency 153 5.9 Binary I/O 156 5.10 Positioning a Stream 157 5.11 For matted I/O 159 5.12 Implementation Details 164...

Global site tag (gtag.js) - Google Analytics