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

前端 未结 5 1835
忘了有多久
忘了有多久 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:57

    fgets does always add a '\0' to the read buffer, it reads at most size - 1 characters from the stream (size being the second parameter) because of this.

    Never use gets as you can never guarantee that it won't overflow any buffer that you give it, so while it technically does always terminate the read string this doesn't actually help.

提交回复
热议问题