获取终端输入字符串scanf,gets,fgets
获取终端中输入字符串,我们常用getchar(),scanf(),gets(),和fgets()函数实现,这里简单汇总一下:
scanf() 函数可以获取到终端字符串,但是不能识别到空格字符,遇到空格会终止,空格后面的字符串会在缓冲区中,下次获取时会继续用。
gets()函数以回车字符进行标识,获取用户输入字符串以回车为终止符。
scanf和gets函数都是不安全的,如果目标字符串缓冲区无法存储我们输入的字符串,会导致段错误
fgets()函数限定了获取字符串的长度,如果输入字符串过多,依然可以只获取限定长度的字符串,剩下的字符串还在输入缓冲区中。
测试demo以及输入测试描述:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//scanf输入遇到空格会当成结束符
//gets是遇到回车结束 fgets其实对gets的一些完善扩展
int main()
{
//scanf 读取终端内容时 遇到空格会终止
// 终止后剩余的结果其实还在终端缓冲区中,会在后面读取缓冲区继续生效
char data[20];
printf("please test of scanf :\n");
scanf("%s", data);
printf("scanf func get data is [%s] \n", data);
memset(data, 0, 20);
printf("please test of space scanf :\n");
scanf("%s", data);
printf("scanf func get data is [%s] \n", data);
memset(data, 0, 20);
fflush(stdin); //可以刷新缓冲区 一般用在读数据后没读完
//gets可以读取换行前的所有字符,但是gets是不安全的,如无法保证长度越界
printf("please test of gets \n");
gets(data);
printf("gets func get data is [%s] \n", data);
memset(data, 0, 20);
printf("please test of space gets :\n");
gets(data);
printf("gets func get data is [%s] \n", data);
memset(data, 0, 20);
//使用fgets函数 对gets函数的扩展,限定了读取数据的长度
printf("plese test of fgets : \n");
fgets(data, 20, stdin); //字符串地址 字符串长度 读入的文件
printf("fgets func get data is : [%s]", data);
memset(data, 0, 20);
printf("please test of len out of 20 fgets:\n");
fgets(data, 20, stdin);
printf("fgets func get data is : [%s] \n", data);
memset(data, 0, 20);
fflush(stdin);
return 0;
}
/********************************************
输出的结果:
hlp@ubuntu:~/com_port$ ./in
please test of scanf : ==>scanf正常输入
12334
scanf func get data is [12334] ==》打印获取终端的输入
please test of space scanf : ==》scanf输入带空格的字符串
123 1231
scanf func get data is [123] ==》发现只打印了空格前的字符串
please test of gets
gets func get data is [ 1231 ] ==》没有经过手动输入, get函数直接获取到终端缓冲区中已经存在的字符串
please test of space gets : ==》get函数测试带空格的字符串
123 123 412
gets func get data is [123 123 412] ==》get函数能打印带空格的字符串,终结符以回车为准
plese test of fgets : ==》测试fget函数获取,在长度内,发现获取到终端内的回车字符
123123 123 1
fgets func get data is : [123123 123 1
]please test of len out of 20 fgets: ==》如果超过fgets函数参数限制的长度,获取到输入的固定长度字符19个字符
12312 1231231 12312 123123 12312123
fgets func get data is : [12312 1231231 12312]
**************************************************/
/********************************************
可以看出,输入字符串过长,scanf和get函数如果获取长度过多,都会引发段错误
hlp@ubuntu:~/com_port$ ./in
please test of scanf :
1
scanf func get data is [1]
please test of space scanf :
1
scanf func get data is [1]
please test of gets
gets func get data is []
please test of space gets :
111111111111111111111111111111111111111111
gets func get data is [111111111111111111111111111111111111111111]
plese test of fgets :
12
fgets func get data is : [12
]please test of len out of 20 fgets:
12
fgets func get data is : [12
*** stack smashing detected ***: <unknown> terminated
Aborted (core dumped)
hlp@ubuntu:~/com_port$ ./in
please test of scanf :
111111111111111111111111111111111111111
scanf func get data is [111111111111111111111111111111111111111]
please test of space scanf :
11111111111111111111111111111111111
scanf func get data is [11111111111111111111111111111111111]
please test of gets
gets func get data is []
please test of space gets :
1
gets func get data is [1]
plese test of fgets :
1
fgets func get data is : [1
]please test of len out of 20 fgets:
1
fgets func get data is : [1
*** stack smashing detected ***: <unknown> terminated
Aborted (core dumped)
**************************************************/
/********************************************
可以看出,输入字符串过长,如果用fget函数进行获取,会在限制长度内获取,下次继续获取缓冲区中剩余字段
hlp@ubuntu:~/com_port$ ./in
please test of scanf :
1
scanf func get data is [1]
please test of space scanf :
1
scanf func get data is [1]
please test of gets
gets func get data is []
please test of space gets :
1
gets func get data is [1]
plese test of fgets :
1111111111111111111111111111111111111111111111111111111111111111111111111
fgets func get data is : [1111111111111111111]
please test of len out of 20 fgets:
fgets func get data is : [1111111111111111111]
**************************************************/