Search code examples
linuxprintfpopenfgets

fgets not terminating at newline


According to the manpages, fgets writes a stream of characters to an array until it hits a null or newline character. But when I use fgets with a popen() stream with various shell commands, and then print the resulting array with printf, the newlines are retained and the output looks the same way as though I had gone through a terminal. Why is this?


Solution

  • The fgets(3) man page reads:

    If a newline is read, it is stored into the buffer.