从标准输入读取() [英] read() from stdin

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

问题描述

考虑以下代码行:

while((n = read(STDIN_FILENO, buff, BUFSIZ)) > 0)

根据我的理解,read/write 函数是非缓冲 I/O 的一部分.那么这是否意味着 read() 函数每次从 stdio 调用只会读取一个字符?或者换句话说,n 的值将是

As per my understanding read/write functions are a part of non-buffered I/O. So does that mean read() function will read only one character per call from stdio? Or in other words, the value of n will be

    -1  in case of error
n =  0  in case of EOF
     1  otherwise

如果不是,上面的read()函数什么时候返回,为什么?

If it is not the case, when would the above read() function will return and why?

注意:我还认为 read() 会等到它从标准输入成功读取 BUFSIZ 个字符.但是如果可供读取的字符数少于 BUFSIZ 会发生什么?读取会永远等待还是直到 EOF 到达(Unix 上的 Ctrl + D 或 Windows 上的 Ctrl + Z)?

Note: I was also thinking that read() will wait until it successfully reads BUFSIZ number of characters from stdin. But what happens in a case number of characters available to read are less than BUFSIZ? Will read wait forever or until EOF arrives (Ctrl + D on unix or Ctrl + Z on windows)?

另外,假设 BUFSIZ = 100stdin = ACtrl+D(即紧跟在单个字符之后的 EOF).现在 while 循环 将迭代多少次?

Also, lets say BUFSIZ = 100 and stdin = ACtrl+D (i.e EOF immediately following a single character). Now how many times the while loop will iterate?

推荐答案

read() 的行为方式取决于正在读取的内容.对于常规文件,如果您要求 N 个字符,如果它们可用,您将得到 N 个字符,如果文件末尾介入,则小于 N.

The way read() behaves depends on what is being read. For regular files, if you ask for N characters, you get N characters if they are available, less than N if end of file intervenes.

如果 read() 在规范/cooked 模式下从终端读取,tty 驱动程序一次提供一行数据.因此,如果您告诉 read() 获取 3 个字符或 300 个字符,则 read 将挂起,直到 tty 驱动程序看到换行符或终端定义的 EOF 键,然后 read() 将返回该行中的字符数或您请求的字符数,以较小者为准.

If read() is reading from a terminal in canonical/cooked mode, the tty driver provides data a line at a time. So if you tell read() to get 3 characters or 300, read will hang until the tty driver has seen a newline or the terminal's defined EOF key, and then read() will return with either the number of characters in the line or the number of characters you requested, whichever is smaller.

如果 read() 正在以非规范/原始模式从终端读取,则 read 将可以立即访问按键.如果您要求 read() 获取 3 个字符,它可能会返回 0 到 3 个字符,具体取决于输入时间和终端的配置方式.

If read() is reading from a terminal in non-canonical/raw mode, read will have access to keypresses immediately. If you ask read() to get 3 characters it might return with anywhere from 0 to 3 characters depending on input timing and how the terminal was configured.

read() 在面对信号时会有不同的表现,返回的字符数少于请求的字符数,或者如果在任何字符到达之前信号中断读取,则返回 -1 并将 errno 设置为 EINTR.

read() will behave differently in the face of signals, returning with less than the requested number of characters, or -1 with errno set to EINTR if a signal interrupted the read before any characters arrived.

read() 的行为会有所不同.如果没有立即可用的输入,则 read() 将返回 -1 并将 errno 设置为 EAGAIN 或 EWOULDBLOCK.这适用于套接字.

read() will behave differently if the descriptor has been configured for non-blocking I/O. read() will return -1 with errno set to EAGAIN or EWOULDBLOCK if no input was immediately available. This applies to sockets.

如您所见,当您调用 read() 时,您应该准备好迎接惊喜.你不会总是得到你请求的字符数,你可能会得到像 EINTR 这样的非致命错误,这意味着你应该重试 read().

So as you can see, you should be ready for surprises when you call read(). You won't always get the number of characters you requested, and you might get non-fatal errors like EINTR, which means you should retry the read().

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

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