When using fgets to read from stdin how do you stop reading from stdin?

From the docs:

The C library function char fgets(char str, int n, FILE *stream) reads a line from the specified stream and stores it into the string pointed to by str. It stops when either (n-1) characters are read, the newline character is read, or the end-of-file is reached, whichever comes first.

-> https://www.tutorialspoint.com/c_standard_library/c_function_fgets.htm

So it stops under those conditions. If you want to reveal why you want it to stop or have a C code sample, add that here.

I thought newline is equivalent to hitting enter on the keyboard. Is that correct? With this while loop it does not stop when I hit enter. I have to hit Ctrl-D or fill up the buffer to make it stop reading.

while(fgets(line, sizeof line, stdin))
{
}

I think you need to read the tutorial again. You have a while() and no check for break, etc.

Remember I do not know what you want to happen but the code you supplied would behave as you said.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.