阅读()从标准输入不忽略换行符 [英] read() from stdin doesn't ignore newline

查看:126
本文介绍了阅读()从标准输入不忽略换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下条件语句从标准输入读取。

I am using the following conditional statement to read from standard input.

if ((n = read(0,buf,sizeof(buf))) != 0)

当输入从标准输入数据,一般都做,当用户presses进入。但阅读()在这种情况下考虑的'\\ n'作为输入太 N = 1 和条件不评估为false。有没有一种方法,使有条件的评估为false,当用户presses输入(不输入任何东西)从BUF检查的内容除了标准输入。是否有任何其他功能比其他阅读(),我可能会使用此目的?

When inputting data from standard input, generally the user presses enter when done. But read() considers '\n' as input too in which case n = 1 and the conditional doesn't evaluate to false. Is there a way to make the conditional evaluate to false when the user presses enter (without entering anything) on standard input apart from checking the contents of buf. Is there any other function other than read() that I might use for this purpose??

对于这个问题,有什么可以为读取以确定输入端,当输入来自标准输入(stdin)?一个办法

For that matter, what can be a way for read to determine end of input when the input comes from standard input (stdin)?

推荐答案

您问:

当输入从标准输入数据,一般都做,当用户presses进入。但阅读()认为的'\\ n'作为输入过在这种情况下,N = 1和条件不评估为假。

When inputting data from standard input, generally the user presses enter when done. But read() considers '\n' as input too in which case n = 1 and the conditional doesn't evaluate to false.

第一点是千真万确的。回车键就相当于换行键,因此当用户presses输入,键盘产生一个换行符,因此阅读()函数返回字符。至关重要的是,它确实做到这一点。

The first point is certainly true. The enter key is equivalent to the newline key, so when the user presses enter, the keyboard generates a newline character, and the read() function therefore returns that character. It is crucial that it does do that.

因此​​,你的条件是误导 - 空线将包括换行,因此,该字​​节计数将是一个。事实上,只有获得阅读()调用返回0时,标准输入是键盘,这就是键入EOF字符的一种方法 - 通常控制 - D开DOS的Unix,控制-Z。在Unix上,该字符是终端驱动程序PTED为'previous输入数据发送到程序即使没有新行还为INTER $ P $。而且,如果用户键入闲来无事就行了,然后从回读()将为零。如果输入的是从一个文件来,然后最后一个数据被读取后,后续的读取会返回0字节。

Therefore, your condition is misguided - an empty line will include the newline and therefore the byte count will be one. Indeed, there's only one way to get the read() call to return 0 when the standard input is the keyboard, and that's to type the 'EOF' character - typically control-D on Unix, control-Z on DOS. On Unix, that character is interpreted by the terminal driver as 'send the previous input data to the program even if there is no newline yet'. And if the user has typed nothing else on the line, then the return from read() will be zero. If the input is coming from a file, then after the last data is read, subsequent reads will return 0 bytes.

如果输入的是从管道的到来,在管道中的所有数据被读取在此之后,阅读()通话将阻塞,直到最后一个文件描述符,可以写到管道是封闭的;如果该文件描述符是在当前进程,那么阅读()将永远挂起,即使挂起的进程将永远无法写( )来的文件描述符 - 假设当然是单线程的过程中,

If the input is coming from a pipe, then after all the data in the pipe is read, the read() call will block until the last file descriptor that can write to the pipe is closed; if that file descriptor is in the current process, then the read() will hang forever, even though the hung process will never be able to write() to the file descriptor - assuming a single-threaded process, of course.

这篇关于阅读()从标准输入不忽略换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆