简体   繁体   English

K&R的C编程语言:第1.5节字符输入和输出

[英]K&R's The C Programming Language: Section 1.5 Character Input and Output

Definition: A text stream is a sequence of characters divided into lines; 定义: 文本流是分成行的一系列字符; each line consists of zero or more characters followed by a newline character. 每行包含零个或多个字符,后跟换行符。 This definition is verbatim from K&R. 这个定义是K&R的逐字记录。

I am reading K&R The C Programming Language, and I am a little confused by how the input and output is occuring. 我正在阅读K&R C编程语言,对输入和输出的发生方式有些困惑。 So I wrote a little program to see how it works, this is what I get: 所以我写了一个小程序来看看它是如何工作的,这就是我得到的:

在此处输入图片说明

The while loop gets a character and saves it as char c then it executes putchar(c) and so on until I hit return (ending the text stream ), the part that confuses me is: as I type the input text stream putchar(c) doesn't appear to be working (it is certainty not printing c with each iteration of the loop), but as soon as I hit return I get back what I typed. while循环获取一个字符并将其保存为char c然后执行putchar(c) ,依此类推,直到我按下return(结束文本流 )为止,令我感到困惑的部分是:当我键入输入文本流 putchar(c)似乎不起作用(可以肯定的是,在每次循环迭代中不打印c ),但是一旦我按下return键,我就会得到输入的内容。

I think the following is happening, but I hope someone can confirm or correct it. 我认为以下情况正在发生,但我希望有人可以确认或纠正。 as soon as I execute inputOutput.exe in the first line of the Command Prompt it is set to receive an input text stream (this is not a result of the program, but of how the command prompt works.) As I type text into the Command Prompt each character is being sent to an "output text stream" by putchar(c) , but that stream is not visible from the Command Prompt until after i hit return on my keyboard, at which time the Command Prompt ends the input mode, it then executes the last putchar(c) which invokes the current output text stream to be presented on the Command Prompt. 在命令提示符的第一行中执行inputOutput.exe ,它将立即设置为接收输入文本流 (这不是程序的结果,而是命令提示符的工作方式。)命令提示符每个字符都由putchar(c)发送到“输出文本流”,但是直到我按键盘上的return键后,命令提示符才显示该流,这时命令提示符结束输入模式,然后,它执行最后一个putchar(c) ,后者调用要在命令提示符下显示的当前输出文本流 If this is correct then, it seems to me the way in which the code manifest itself is governed by the rules of the command prompt and not so much by the C language, is this correct? 如果这是正确的,那么在我看来,代码清单本身的方式受命令提示符规则的控制,而不是受C语言的约束,这是正确的吗?

That is an artifact of line-buffering on input. 那是在输入上进行行缓冲的产物。 Many Consoles/Programs do that, sometimes even allowing advanced input editing. 许多控制台/程序都这样做,有时甚至允许高级输入编辑。 Try putting your console/program in direct mode / unbuffered. 尝试将控制台/程序置于直接模式/无缓冲。

The reason putchar(c) doesn't immediately show the character on screen is buffering. putchar(c)没有立即在屏幕上显示字符的原因是正在缓冲。 You could add: 您可以添加:

fflush(stdout);

right after 之后

putchar(c);

and output will make more sense. 和输出将更有意义。

You can also turn off buffering, but usually you want it on. 您也可以关闭缓冲,但是通常需要打开缓冲。

The reason you want buffering, is if you're writing out a lot of data, it will be a lot faster for C-runtime to buffer the text and flush it out all at once (or a buffer at a time). 想要缓冲的原因是,如果要写出大量数据,C运行时缓冲文本并一次全部清空(或一次清空)文本的速度会快得多。

It is normal for UI-type programs in C, to do: 对于C语言中的UI类型的程序,通常这样做:

printf(... printing things);
printf(... printing some more things);
...
fflush(stdout); /* at this point everything will be seen by user */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM