Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent inputs being flushed into output?

I'm on Ubuntu. When I run ghci on Terminal and do this:

Prelude Control.Monad System.IO> forever $ getChar >>= print

The result is something like this:

a'a'
b'b'
C'C'
%'%'
\'\\'
1'1'
''\''
"'"'
^X'\CAN'
^?'\DEL'
^CInterrupted.

That is, the characters I type in my keyboard are being flushed into output. How can I prevent this and have only print as the writer?

like image 985
Dannyu NDos Avatar asked Aug 10 '19 23:08

Dannyu NDos


People also ask

What is output Flushing?

Flushing output on a buffered stream means transmitting all accumulated characters to the file. There are many circumstances when buffered output on a stream is flushed automatically: When you try to do output and the output buffer is full.

What is an input buffer?

When referring to computer memory, the input buffer is a location that holds all incoming information before it continues to the CPU for processing. Input buffer can be also used to describe other hardware or software buffers used to store information before it is processed.

How do I clear my output buffer?

The function fflush(stdin) is used to flush or clear the output buffer of the stream. When it is used after the scanf(), it flushes the input buffer also. It returns zero if successful, otherwise returns EOF and feof error indicator is set.

How do you clear a buffer in C++?

Using “ fflush(stdin) ”: Typing “fflush(stdin)” after “scanf()” statement, also clears the input buffer but generally it's use is avoided and is termed to be “undefined” for input stream as per the C++11 standards.


1 Answers

To prevent input being flushed into the output (or "echoed"), use hSetEcho stdin False.

Prelude> import System.IO
Prelude System.IO> import Control.Monad
Prelude System.IO Control.Monad> hSetEcho stdin False
Prelude System.IO Control.Monad> forever $ getChar >>= print
'a'
'\n'
'b'
'c'

This can be used to do things like read in a password.

like image 165
4castle Avatar answered Oct 19 '22 01:10

4castle