Does fgets() always null-terminate the string it returns?

后端 未结 2 1178
庸人自扰
庸人自扰 2021-01-24 07:50

Is this safe to do? Does fgets terminate the buffer with null or should I be setting the 20th byte to null after the call to fgets and before I call

2条回答
  •  再見小時候
    2021-01-24 08:26

    Yes fgets() always properly null-terminates the buffer. From the man page:

    The fgets() function reads at most one less than the number of characters specified by n from the given stream and stores them in the string s. Reading stops when a newline character is found, at end-of-file or error. The newline, if any, is retained. If any characters are read and there is no error, a '\0' character is appended to end the string.

提交回复
热议问题