1. gets函数
gets函数是不安全的,不推荐使用。如下代码:
#include <stdio.h>
int main(void)
{
char name[5];
gets(name);
puts(name);
return 0;
}
编译运行:
由此看到编译器给出的警告信息:使用gets函数是不安全的,不推荐使用。
再运行一次,输入较多字符时:
gets不检查预留存储区是否能够容纳实际输入的数据。多出来的字符简单的溢出到相邻的存储区,可能会导致错误。
2. fgets函数
(1)函数原型:
char * fgets ( char * str, int num, FILE * stream );
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 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