前言的闲话以及第一章的入门(四).1

1.5 Character Input and Output

The model of input and output supported by the standard library is very simple. Text input or output, regardless of where it originates or where it goes to, is dealt with as streams of characters. A text stream is a sequence of characters divided into lines; each line consists of zero or more characters followed by a newline character. It is the responsibility of the library to make each input or output stream confirm this model;

标准库提供的输入/输出模型非常简单。无论文本从何处输入,输出到何处,其输入/输出都是按照字符流的方式处理。文本流是由多行字符构成的字符序列,而每行字符则由0个或多个字符组成,行末是一个换行符。标准库负责使每个输入/输出流都能够遵守这一模型.

The standard library provides several functions for reading or writing one character at a time, of which getchar and putchar are the simplest. Each time it is called, getchar reads the next input character from a text stream and returns that as its value. That is, after

标准库提供了一次读/写一个字符的函数,其中最简单的是getchar putchar 两个函数。每次调用时,getchar函数从文本流中读入下一个输入字符,并将其作为结果值返回。

c = getchar();

the variable c contains the next character of input. The characters normally come from the keyboard;  

变量c 中将包含输入流中的下一个字符。这种字符通常是通过键盘输入的。

The function putchar prints a character each time it is called:

putchar(c);

prints the contents of the integer variable c as a character, usually on the screen. Calls to putchar and printf may be interleaved;

将把整型变量c 的内容以字符的形式打印出来,通常是显示在屏幕上。putchar printf这两个函数可以交替调用

 

1.5.1 File Copying

The simplest example is a program that copies its input to its output one character at a time:

read a character

while (charater is not end-of-file indicator)

output the character just read

read a character

Converting this into C gives:

#include <stdio.h>

/* copy input to output; 1st version */

main()

{

    int c;

    c = getchar();

    while (c != EOF) {

        putchar(c);

        c = getchar();

    }

}

The problem is distinguishing the end of input from valid data. The solution is that getchar returns a distinctive value when there is no more input, a value that cannot be confused with any real character. This value is called EOF, for ``end of file''. We must declare c to be a type big enough to hold any value that getchar returns. We can't use char since c must be big enough to hold EOF in addition to any possible char. Therefore we use int.

这里需要解决如何区分文件中有效数据与输入结束符的问题。C语言采取的解决方法是:在没有输入时,getchar 函数将返回一个特殊值,这个特殊值与任何实际字符都不同。这个值称为EOFend of file,文件结束)。我们在声明变量c 的时候,必须让它大到足以存getchar函数返回的任何值。这里之所以不把c声明成char类型,是因为它必须足够大,除了能存储任何可能的字符外还要能存储文件结束符EOF。因此,我们将c声明成int类型。

 

EOF is an integer defined in <stdio.h>, but the specific numeric value doesn't matter as long as it is not the same as any char value. By using the symbolic constant, we are assured that nothing in the program depends on the specific numeric value.

EOF 定义在头文件<stdio.h>中,是个整型数,其具体数值是什么并不重要,只要它与任何char类型的值都不相同即可。这里使用符号常量,可以确保程序不需要依赖于其对应的任何特定的数值。

 

If the assignment of a character to c is put inside the test part of a while loop, the copy program can be written this way:

如果将为c赋值的操作放在while循环语句的测试部分中,上述字符复制程序便可以改写成下列形式:

#include <stdio.h>

/* copy input to output; 2nd version */

main()

{

    int c;

    while ((c = getchar()) != EOF)

    putchar(c);

}

The while gets a character, assigns it to c, and then tests whether the character was the end-of-file signal. If it was not, the body of the while is executed, printing the character. The while then repeats. When the end of the input is finally reached, the while terminates and so does main.

在该程序中,while 循环语句首先读一个字符并将其赋值给c,然后测试该字符是否为文件结束标志。如果该字符不是文件结束标志,则执行while语句体,并打印该字符。随后重复执行while语句。当到达输入的结尾位置时,while循环语句终止执行,从而整个main数执行结束。

 

This version centralizes the input - there is now only one reference to getchar - and shrinks the program. The resulting program is more compact, and, once the idiom is mastered, easier to read. You'll see this style often. (It's possible to get carried away and create impenetrable code, however, a tendency that we will try to curb.)

以上这段程序将输入集中化,getchar函数在程序中只出现了一次,这样就缩短了程序,整个程序看起来更紧凑。习惯这种风格后,读者就会发现按照这种方式编写的程序更易阅读。我们经常会看到这种风格。(不过,如果我们过多地使用这种类型的复杂语句,编写的程序可能会很难理解,应尽量避免这种情况。)

 

The parentheses around the assignment, within the condition are necessary. The precedence of != is higher than that of =, which means that in the absence of parentheses the relational test != would be done before the assignment =. So the statement

 while语句的条件部分来说,赋值表达式两边的圆括号不能省略。不等于运算符!=优先级比赋值运算符=的优先级要高,这样,在不使用圆括号的情况下关系测试!=将在赋值=操作之前执行。

c = getchar() != EOF

is equivalent to

c = (getchar() != EOF) 

This has the undesired effect of setting c to 0 or 1, depending on whether or not the call of getchar returned end of file.

该语句执行后,c的值将被置为0 1(取决于调用getchar函数时是否碰到文件结束标志),这并不是我们所希望的结果。

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值