What does fgets () actually wait for user input if pointed to by stdin?

I am new to C and I am trying to understand what happens in fgets() when pointed to by stdin .

Basically, my question is, excuse me, I really could not understand fgets() :

If I point to a file that will be used in fgets() , then fgets() will be read from this place to the exact \n , EOF or specified limit-1

So, why does it behave differently if I point to stdin , in the sense that it makes him wait for user input, and not just read nothing and therefore return NULL ?

thank you for your time

+5
source share
2 answers

fgets() is read from the stream of arguments. If this stream is attached to a device or pipe, it is blocked until the input from the device / pipe is received or until the end of the file is detected.

stdin usually bound to a terminal. Reading from the terminal reads any pending input, and in the case of fgets() continues reading until a newline is entered or a sufficient number of characters are entered. There is one additional level: the kernel driver for the terminal does its own buffering by default, forcing the input operation to block until a new line is entered, even if more characters are typed than fgets() expects. These extra characters are pending in the terminal buffer.

The terminal can be configured for raw mode (as opposed to the default ready mode) with the stty system call (on Posix systems). This will remove buffering in the device driver, but buffering will be performed on the FILE * stream. fgets() can only access characters from the stream after doing this buffering. Device-bound streams are usually line-buffered by default, making the stream buffering match the device buffering. If you set both raw mode and the stream to be unbuffered, the characters will be available to fgets() as they are entered, and fgets() will stop reading when it receives a new line or when it reads size-1 characters.

Note also that you can enter the end of the file in cooked mode by typing Control-D on unix and OS / X and Control-Z Enter on Windows.

+4
source

If I point to a file that will be used in fgets (), fgets () will start reading from this location to \ n, EOF, or the specified limit of-1

It does not read from this location, it reads from this file.

So, why does this behave differently if I point to stdin, in the sense that it makes him wait for user input and not just read nothing and therefore return NULL?

It does not behave differently. The fgets function accepts only a pointer to a file and always reads from this file.

I suspect you are confusing fgets with some other function that does something else. The fgets function is and is the only function to block reading from a file.

+2
source

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


All Articles