从stdin读取() [英] read() from stdin

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

问题描述

考虑以下代码行:

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

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

  -1如果出现错误
n = 0 EOF
1否则

如果不是这样的话,上面的时间是什么时候 read()函数会返回以及为什么?



注意:我也在想 read() 将一直等到成功读取 BUFSIZ stdin中的字符数。但是在一个案例中可以读取的字符数小于 BUFSIZ ?将永远等待或直到EOF到达(在UNIX上 Ctrl + D 或在Windows上 Ctrl + Z )?



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

解决方案

read()的行为方式取决于所读取的内容。对于常规文件,如果您要求N个字符,如果可用,则获得N个字符,如果文件结尾介入,则小于N.



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



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



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



<如果描述符已配置为非阻塞I / O,则read()的行为会有所不同。如果没有立即可用的输入,read()将返回-1,并将errno设置为EAGAIN或EWOULDBLOCK。这适用于套接字。



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


Consider the following line of code:

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

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

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

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)?

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?

解决方案

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.

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.

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() will behave differently in the face of signals, returning with less than the requesting number of characters, or -1 with errno set to EINTR if a signal interrupted the read before any characters arrived.

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.

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().

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

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