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

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

问题描述

我正在使用以下条件语句从标准输入中读取数据.

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

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

从标准输入输入数据时,通常用户在完成后按回车键.但是 read() 也将 ' ' 作为输入,在这种情况下 n = 1 并且条件不会评估为假.除了检查 buf 的内容外,有没有办法在用户在标准输入上按 Enter(不输入任何内容)时使条件评估为 false.除了 read() 之外,还有其他函数可以用于此目的吗?

When inputting data from standard input, generally the user presses enter when done. But read() considers ' ' 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) 时,有什么方法可以让 read 确定输入结束?

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

推荐答案

你问:

从标准输入输入数据时,通常用户在完成后按回车键.但是 read() 也将 ' ' 作为输入,在这种情况下 n = 1 并且条件不会评估为 false.

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

第一点肯定是对的.enter 键等同于换行键,因此当用户按下 Enter 键时,键盘会生成一个换行符,因此 read() 函数会返回该字符.做到这一点至关重要.

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.

因此,您的条件被误导了 - 空行将包含换行符,因此字节数将为 1.事实上,当标准输入是键盘时,只有一种方法可以让 read() 调用返回 0,那就是输入EOF"字符——在 Unix 上通常是 control-D,control-DOS 下的 Z.在 Unix 上,该字符被终端驱动程序解释为即使没有换行符也将先前的输入数据发送给程序".如果用户没有输入任何其他内容,则 read() 的返回值将为零.如果输入来自文件,那么在读取最后一个数据后,后续读取将返回 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.

如果输入来自管道,那么在读取管道中的所有数据后,read() 调用将阻塞,直到最后一个可以写入管道的文件描述符关闭;如果该文件描述符在当前进程中,则 read() 将永远挂起,即使挂起的进程永远无法将 write() 写入文件描述符- 当然,假设是单线程进程.

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天全站免登陆