为什么不推荐使用 fgets 函数? [英] Why is the fgets function deprecated?
问题描述
来自GNU C 编程教程:
fgets
(文件获取字符串")函数类似于gets功能.此函数已弃用 -- 这意味着它已过时强烈建议您不要使用它——因为它是危险的.这是危险的,因为如果输入数据包含空值性格,你看不出来.除非你知道数据,否则不要使用 fgets
不能包含空值.不要用它来读取用户编辑的文件因为,如果用户插入空字符,您应该正确处理或打印清晰的错误信息.一直使用如果可以,getline
或 getdelim
代替 fgets
.
The
fgets
("file get string") function is similar to the gets function. This function is deprecated -- that means it is obsolete and it is strongly suggested you do not use it -- because it is dangerous. It is dangerous because if the input data contains a null character, you can't tell. Don't usefgets
unless you know the data cannot contain a null. Don't use it to read files edited by the user because, if the user inserts a null character, you should either handle it properly or print a clear error message. Always usegetline
orgetdelim
instead offgets
if you can.
我认为 fgets
函数在遇到