c语言获取终端输入字符串的函数scanf,gets,fgets

简介: c语言获取终端输入字符串的函数scanf,gets,fgets

获取终端输入字符串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]
**************************************************/
目录
相关文章
|
21小时前
|
Java C语言 C++
定义C语言的int main()函数
定义C语言的int main()函数
|
2天前
|
存储 移动开发 C语言
技术心得记录:嵌入式开发中常用到的C语言库函数
技术心得记录:嵌入式开发中常用到的C语言库函数
|
2天前
|
Java 程序员 Linux
探索C语言宝库:从基础到进阶的干货知识(类型变量+条件循环+函数模块+指针+内存+文件)
探索C语言宝库:从基础到进阶的干货知识(类型变量+条件循环+函数模块+指针+内存+文件)
9 0
|
2天前
|
C语言
C语言实现猜数字游戏:代码详解与函数解析
C语言实现猜数字游戏:代码详解与函数解析
5 0
|
2天前
|
程序员 C语言
C语言内存管理:malloc、calloc、realloc与free函数详解
C语言内存管理:malloc、calloc、realloc与free函数详解
3 0
|
3天前
|
C语言
C语言中的函数指针、指针函数与函数回调
C语言中的函数指针、指针函数与函数回调
6 0
|
3天前
|
存储 C语言
C语言中的变量与函数详解
C语言中的变量与函数详解
3 0
|
7月前
|
C语言
【C语言】用函数递归的方法解决汉诺塔问题
【C语言】用函数递归的方法解决汉诺塔问题
37 0
|
算法 程序员 C语言
C语言基础(有关三个数比较大小、冒泡排序、最大公约数、和有关某个数x的绝对值的n次方除于n的阶乘问题的函数求解法;和阶乘函数递归方法;和数组作函数参数的
C语言基础(有关三个数比较大小、冒泡排序、最大公约数、和有关某个数x的绝对值的n次方除于n的阶乘问题的函数求解法;和阶乘函数递归方法;和数组作函数参数的
|
C语言
字符串逆序(C语言版 函数,递归方法)
字符串逆序(C语言版 函数,递归方法)
105 0
字符串逆序(C语言版 函数,递归方法)