一、gets()函数

原型:char *gets(char *str);
头文件:stdio.h

例1

#include <stdio.h>

int main()
{
char str[10];
gets(str);
puts(str);

return 0;
}

(1)在Windows系统中的运行结果

hello
hello

(2)在Linux中用GCC进行编译

noilinux@ubuntu:~/Desktop$ gcc test.c -o test
test.c: In function ‘main’:
test.c:6:5: warning: ‘gets’ is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations]
gets(str);
^
/tmp/cc0hPgqA.o:在函数‘main’中:
test.c:(.text+0x1d): 警告: the `gets' function is dangerous and should not be used.

运行结果

noilinux@ubuntu:~/Desktop$ ./test
hello
hello

例2

#include <stdio.h>

int main(void)
{
char c;
char s[3];
scanf("%c",&c);
getchar(); // 过滤回车
gets(s);

printf("string=%s\nchar=%c\n", s, c);

return 0;
}

运行结果
在windows下输入:

a
hi

输出

string=hi
char=a

在windows下重新输入

a
uvwxyz

输出

string=uvw
char=x

这里可以看出来,定义了s的长度为3,但是用gets()输入字符串的时候,并不会去检查字符串的长度,所以导致char的值不是a,而是”uvwxyz”中的第四个字符’x’。
不正确使用gets()函数的时候造成的危害是很大的,就像我们刚才看到的那样,a的值被字符串s溢出的值给替换了。
因为gets有不限制输入字符个数的限制,可能会导致不法分子利用这一漏洞造成缓冲区溢出,从而达到破坏的目的。《C Primer Plus》中提到蠕虫病毒就是利用这一漏洞来攻击操作系统。
出于安全考虑,用fgets()来代替gets()。

二、fgets()函数

原型:char * fgets(char * s, int n,FILE *stream);
头文件:stdio.h
fgets()函数读取到它所遇到的第一个换行符的后面,或者读取比字符串的最大长度少一个的字符,或者读取到文件结尾。然后fgets()函数向末尾添加一个空字符以构成一个字符串。如果在达到字符最大数目之前读完一行,它将在字符串的空字符之前添加一个换行符以标识一行结束。

例3

#include <stdio.h>
#define len 5

int main()
{
char c;
char s[len];
scanf("%c", &c);
getchar(); // 过滤回车
fgets(s, len, stdin);

printf("string=%s\nchar=%c\n", s, c);

return 0;
}

运行结果
输入:

a
uvwxyz

输出:

string=uvwx
char=a
这里string=uvwx的后面其实还有一个空字符’\0’没有显示出来。

例4

#include <stdio.h>
#include <string.h>
#define len 100

int main()
{
// stdin,标准输入流,默认是键盘,重定向到文件title.in,scanf或fgets从title.in中读取数据
freopen("title.in", "r", stdin);
// stdout,标准输出流,默认是显示器,重定向到title.out,printf把数据写到title.out中
freopen("title.out", "w", stdout);

char s[len];
fgets(s, len, stdin);
int total = 0;

// 这里只能用strlen,不能用sizeof
// 因为sizeof(s)=sizeof(s)/sizeof(char)=100
for(int i = 0; i < strlen(s); i++)
{
if(s[i] != ' ')
{
total++;
}
}

printf("%d", total);
return 0;
}
少儿编程QQ群:581357582
少儿英语QQ群:952399366

小朋友学C语言(42):gets和fgets_字符串