输入 &IOError:字符缓冲 IO 的面向字节读取 [英] enter & IOError: byte oriented read for character buffered IO
问题描述
在一个答案中我发现这个节等待您的输入并打印它直到您按下回车键:
In the one answer I have found this stanza that waits for your input and prints it until you press enter:
require 'io/console'
require 'io/wait'
loop do
chars = STDIN.getch
chars << STDIN.getch while STDIN.ready? # Process multi-char paste
break if chars == ?\n
STDOUT.print chars
end
然而,为了退出 loop
,我必须按Enter"(换行键 - \n
)两次,或者在它之后按其他东西.
当我尝试再次执行相同的循环(将其复制粘贴到同一个 pry 会话中)时,我得到:
However, in order to exit loop
, I must press "Enter"(key for new line - \n
) twice, or press something else after it.
When I try to execute the same loop again (copy-paste it into the same pry session) I am getting:
IOError: byte oriented read for character buffered IO
字符<
chars << STDIN.getch while STDIN.ready?
cause raising, mentioned above, error. Without this line, ruby just doesn't show any error.
在这两种情况下(有和没有上一行),在循环中:
In both cases (with and without above line), in the loop:
当我按下回车键,然后按下一些字母(例如z")时,我收到了这个错误.
在下一个循环中,上面的字母将显示(没有我的输入).
when I press the enter and then some letter (for example 'z') I'm getting this error.
In the next loop, above letter will show (without my input).
当我按两次回车 - 没有错误,它会退出.
在下一个循环中,当我按下某个字母时,会显示错误.
在下一个循环中,上面的字母将显示
when I press the enter twice - no error, it will exit.
In the next loop when I press some letter, error will show.
In the next loop above letter will show
我记得在 C 或 C++ 中有 flush
以便您可以清空缓冲区.我找到了几种方法,并尝试如下:
I remember that in C or C++ there was flush
so you can empty the buffer. I have found s few methods, and tried like this:
loop do
STDIN.ioflush
STDOUT.ioflush
STDOUT.iflush
STDIN.iflush
STDOUT.oflush
STDIN.oflush
chars = STDIN.getch
chars << STDIN.getch while STDIN.ready?
break if chars == ?\n
STDOUT.print chars
end
但是没有用.
如何使用输入和第二个字母来解决此问题&IOError
.我认为两者在某种程度上是相关的.
How to solve this behavior with enter and 2nd letter & IOError
. I think both are correlated in some way.
推荐答案
该答案中的代码是我为个人使用而编写的类似 highline 的库的简化版本.我自己从未遇到过那个特定的错误,但这可能是因为我实际上使用的东西略有不同.我的实际代码更像是这样:
The code from that answer is a simplified version of a highline-like library I wrote for personal use. I never encountered that specific error myself, but it might be due to the fact I'm actually using something slightly different. My actual code goes something more like this:
require 'io/console'
require 'io/wait'
catch(:done) do
loop do
chars = STDIN.getch
chars << STDIN.getch while STDIN.ready? # Process multi-char paste
throw :done if ["\r", "\n", "\r\n"].include?(chars)
STDOUT.print chars
end
end
STDOUT.print "\n"
我也有终止信号处理程序,以防按下 Ctrl+C(终止进程)或 Ctrl+D(输入结束);第一个中断程序,第二个被连接起来做出反应,就像按下了 Enter 键一样.
I also have kill-signal handler, in case Ctrl+C (kill process) or Ctrl+D (end of input) get pressed; the first interrupts the program, and the second is wired to react as if enter was pressed.
确切的行为可能取决于操作系统(我在 OSX 和 FreeBSD 上使用它),因为回车键可以返回任何 "\r"
, "\n"
或 "\r\n"
取决于操作系统:
The precise behavior might depend on the OS (I use it on OSX and FreeBSD), since the enter key could return any of "\r"
, "\n"
or "\r\n"
depending on the OS:
这篇关于输入 &IOError:字符缓冲 IO 的面向字节读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!