Does fgets() always terminate the char buffer with \0?

前端 未结 5 1838
忘了有多久
忘了有多久 2021-02-01 21:02

Does fgets() always terminate the char buffer with \\0 even if EOF is already reached? It looks like it does (it certainly does in the implementation presented in the ANSI K&

5条回答
  •  故里飘歌
    2021-02-01 21:50

    Yes it does. From CPlusPlus.com

    Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or a the End-of-File is reached, whichever comes first.

    A newline character makes fgets stop reading, but it is considered a valid character and therefore it is included in the string copied to str.

    A null character is automatically appended in str after the characters read to signal the end of the C string.

提交回复
热议问题