Fgetc (stdin) in loop creates weird behavior

I have this code

while(1){ printf("hello world !\n"); fgetc(stdin); } 

when this is done, and I enter the letter as follows:

 hello world ! a 

it ignores fgetc (stdin) in the next loop and prints the hello world twice without waiting for input.

 hello world ! a hello world ! hello world ! a hello world ! hello world ! a hello world ! hello world ! a hello world ! hello world ! 

I tried putting fflush (stdin) before or after fgetc (stdin), but it still produces the same behavior, what am I doing wrong?

+2
source share
4 answers

Terminals tend to be buffered lines, which means that the contents of the stream are available in turn.

So, when fgetc starts reading from STDIN, it reads the complete line, which includes the newline character that ended this line. This is the second character you are reading.

As for fflush , it is for fflush output buffers, not input buffers.

So what you want to do is clear the input buffer by reading it until it is empty or simply explicitly ignoring newline characters.

+4
source

This is because you are actually entering two characters: 'a' and a new line. In addition, since the terminal is usually buffered by line, your program will only see your input after you click on a new line. It will also be useful to enter a longer line of text.

If you want to change this behavior, you have two options: reading whole lines (i.e. all characters to a new line or the end of the file) or switching the terminal to non-canonical mode. The latter makes sense if you are working on an interactive terminal application, such as a text editor. See termios manpage for more details. In short, you will want to set the MIN and TIME values ​​to zero in order to read data from the terminal immediately after receiving the data. If you go this route, make sure that you turn off the terminal when exiting, including due to the reception of a signal.

fflush() affects the output, not the input.

+7
source

There are two characters: a and \n (new line). Your loop reads, reads a , then sings and prints "hello world!". Then he sees \n and sings and prints "hello world!". When you type a + \n into the terminal, it stores two characters in the stdin buffer. fgetc(stdin); will be read from stdin if char is present, otherwise it waits until char is added to the buffer.

Since the terminals are buffered by line (i.e. do not send content to the program until a new line is reached), you have several options:

  • read the entire line into the buffer, but take only the first character
  • ignore newlines
  • disable row buffering

To disable line buffering, see http://c-faq.com/osdep/cbreak.html and http://www.flipcode.com/archives/_kbhit_for_Linux.shtml and http://ubuntuforums.org/showthread.php? t = 225713 (although I have not tested any code here).

+2
source

You have two characters, 'a' and '\n' . This is the problem because fgetc will only read one character. This is the documentation .

If you enter only '\n' - press enter only - you will have the expected behavior.

Hope this helps!

+1
source

Source: https://habr.com/ru/post/1202464/


All Articles