- 原 型
- char * gets ( char * str );
- 外文名
- gets
- 头文件
- stdio.h(c),cstdio(C++)
- 中文名
- 读取字符串
- 功 能
- 读取字符串
-
gets
2020-06-15 22:30:43gets()函数用来从标准输入设备(键盘)读取字符串直到换行符结束,但***换行符会被丢弃***,然后在末尾添加’\0’字符。其调用格式为: gets(s);gets()函数用来从标准输入设备(键盘)读取字符串直到换行符结束,但***换行符会被丢弃***,然后在末尾添加’\0’字符。其调用格式为:
gets(s); -
gets函数
2019-04-06 21:53:59GETS(3) Linux Programmer’s Manual GETS(3) NAME fgetc, fgets, getc, getchar, gets, ungetc - input of characters and strings SYNOPSIS #include <s...GETS(3) Linux Programmer’s Manual GETS(3)
NAME
fgetc, fgets, getc, getchar, gets, ungetc - input of characters and stringsSYNOPSIS
#include <stdio.h>int fgetc(FILE *stream);
char *fgets(char *s, int size, FILE *stream);
int getc(FILE *stream);
int getchar(void);
char *gets(char *s);
int ungetc(int c, FILE *stream);
DESCRIPTION
fgetc() reads the next character from stream and returns it as an unsigned char cast to an int, or EOF
on end of file or error.getc() is equivalent to fgetc() except that it may be implemented as a macro which evaluates stream
more than once.getchar() is equivalent to getc(stdin).
gets() reads a line from stdin into the buffer pointed to by s until either a terminating newline or
EOF, which it replaces with '\0'. No check for buffer overrun is performed (see BUGS below).fgets() reads in at most one less than size characters from stream and stores them into the buffer
pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into
the buffer. A '\0' is stored after the last character in the buffer.ungetc() pushes c back to stream, cast to unsigned char, where it is available for subsequent read
operations. Pushed-back characters will be returned in reverse order; only one pushback is guaran-
teed.Calls to the functions described here can be mixed with each other and with calls to other input func-
tions from the stdio library for the same input stream.For non-locking counterparts, see unlocked_stdio(3).
RETURN VALUE
fgetc(), getc() and getchar() return the character read as an unsigned char cast to an int or EOF on
end of file or error.gets() and fgets() return s on success, and NULL on error or when end of file occurs while no charac-
ters have been read.ungetc() returns c on success, or EOF on error.
CONFORMING TO
C89, C99, POSIX.1-2001. LSB deprecates gets(). POSIX.1-2008 removes the specification of gets().BUGS
Never use gets(). Because it is impossible to tell without knowing the data in advance how many char-
acters gets() will read, and because gets() will continue to store characters past the end of the
buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets()
instead.It is not advisable to mix calls to input functions from the stdio library with low-level calls to
read(2) for the file descriptor associated with the input stream; the results will be undefined and
very probably not what you want.SEE ALSO
read(2), write(2), ferror(3), fgetwc(3), fgetws(3), fopen(3), fread(3), fseek(3), getline(3),
getwchar(3), puts(3), scanf(3), ungetwc(3), unlocked_stdio(3)COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A description of the project, and
information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/.gets的man手册
-
scanf和gets连用时gets的异常
2020-06-17 15:39:50当scanf和gets连续使用时,位于scanf之后的gets函数无法正常使用。这是因为scanf和gets结束输入的标志存在差异。Scanf和gets连用时gets的异常
scanf作为C语言初学者最常用的一种输入函数,其实存在很多缺陷。作者在自己学习的过程中偶然间发现scanf后直接接gets会出现一个问题,比如说,
int a; char b[20]; scanf("%d",&a); gets(b);
编译时就会发现,gets(b)完全没法正常运行。
要想解决这个问题,我们就要考虑一下scanf是以什么为输入结束的标志的。在这段程序中,首先我们输入a的值,比如说我们令a输入为123。然后,按“回车”结束scanf的整数输入。再然后我们计划输入字符串b。这似乎没有什么问题,但是等一下,我们输入了什么?123,你确定只有123吗?
这时候我们发现:实际上我们在输入的时候并非只输入了“123”,除了这个整数以外,我们还输入了一个“回车”。是的,回车是scanf结束输入的标志,但回车却不会被scanf接收。这个回车虽然被输入了,但却游离在函数之外,直到gets函数出现了。
是的,gets函数直接接受了“回车”。更要命的是,gets函数也是以回车为输入结束的,这就导致gets函数出现了明显的异常。
要想解决这个问题也很简单,在scanf和gets之间加入一个getchar来吸收这个回车就行了。
-
gets()、fgets()、gets_s()、s_gets()
2020-09-10 10:40:13gets()、fgets()、gets_s()、s_gets() 1、gets()1、gets()
在读取字符串时,scanf()和转换说明符%s只能读取一个单词。gets()函数简单易用,它读取整行输入,直至遇到换行符,然后丢弃换行符,存储其余字符,并在这些字符的末尾添加一个空字符使其成为一个C字符串。但是出现一个问题,gets()函数无法检测数组是否装得下输入行。C编程社区的许多人都建议在编程时摒弃gets()。C99标准的委员会把这些建议放入了标准,承认了gets()的问题并建议不要再使用它。好景不长,C11标准委员会采用取了更强硬的态度,直接从标准中废除了gets()函数。
#include <stdio.h> #define STLEN 81 int main(){ char words[STLEN]; puts("Enter a string, please."); gets(words); printf("Your string twice:\n"); printf("%s\n", words); puts(words); puts("Done."); return 0; } /* 运行输出: main.c:6:5: warning: ‘gets’ is deprecated [-Wdeprecated-declarations] /usr/include/stdio.h:638:14: note: declared here main.c:(.text+0x29): warning: the `gets' function is dangerous and should not be used. Enter a string, please. Hello New Your string twice: Hello New Hello New Done. */
2、fgets()
过去通常fgets()来替换gets(),fgets()函数在处理文件输入方面与gets()略有不同。fgets()函数和gets()的区别:
(1)fgets()函数的第2个参数指明了读入字符的最大数量。如果该参数的值是n,那么fgets()将读入n-1个字符,或者读到遇到的第一个换行符为止;
(2)如果fgets()读到一个换行符,会把它存储在字符串中。这点与gets()不同,gets()会丢弃换行符;
(3)fgets()函数的第3个参数指明要读入的文件。如果读入从键盘输入的数据,则以stdin(标准输入)作为参数,该标识符定义在stdio.h中;
#include <stdio.h> #define STLEN 14 int main(void){ char words[STLEN]; puts("Enter a string, please"); fgets(words, STLEN, stdin); printf("Enter string twice (puts(),then fputs()):\n"); puts(words); fputs(words, stdout); puts("Enter another string, please."); fgets(words, STLEN, stdin); printf("Your string twice (puts(),then fputs()):\n"); puts(words); fputs(words, stdout); puts("Done."); return 0; } /* 运行输出: Enter a string, please apple pie Enter string twice (puts(),then fputs()): apple pie apple pie Enter another string, please. strawberry shortcake Your string twice (puts(),then fputs()): strawberry sh strawberry shDone. */
注1:第一行输入,apple pie时,apple pie\n\0被存储在数组中。puts()显示该函数字符串时又在末尾添加了换行符,因此apple pie后面有一行空行。fputs()不在字符串末尾添加换行符,所以并未打印出空行。
注2:第二行输入,strawberry shortcake,超出了大小的限制,strawberry sh\0存储在数组中。puts()函数会在待输出字符串末尾添加一个换行符,而fputs()不会这样做。
3、gets_s()
gets_s()和fgets()的区别:
(1)gets_s()只从标准输入中读取数据,所以不需要第3个参数;
(2)如果gets_s()读到换行符,会丢弃它而不是存储它;这里和gets()函数几乎一样,完全可以用gets_s()替换gets()。
(3)如果gets_s()读到最大字符数都没有读到换行符或文件结尾,会执行以下几步。首先把目标函数数组中的首字符设置为空字符,读取并丢弃随后的输入直至读到换行符或文件结尾,然后返回空指针。接着,调用依赖实现的“处理函数”(或你选择的其他函数),可能会中止或退出程序。
使用gets()函数不安全,它会擦除现有数据,存在安全隐患。gets_s()函数很安全,但是,如果并不希望程序终止或退出,就要知道如何编写特殊的“处理函数”。
所以,当输入与预期不符时,gets_s()完全没有fgets()函数方便、灵活。也许这也是gets_s()只作为C库的可选扩展的原因之一。
4、s_gets()
自定义函数,要求是:读取整行输入并用空字符代替换行符,或者读取一部分输入,并丢弃其余部分;
//初级版: char* s_gets(char* st, int n){ char* ret_val; int i = 0; ret_val = fgets(st, n, stdin); if (ret_val){ while (st[i] != '\n' && st[i] != '\0') i++; if(st[i] == '\n') st[i] = '\0'; else while(getchar() != '\n') continue; } return ret_val; } //高级版:(包含头文件string.h) char* s_gets(char* st, int n) { char *ret_val; char *find; ret_val = fgets(st, n, stdin); if (ret_val){ find = strchr(st, '\n'); if (find) *find = '\0'; else while (getchar() != '\n') continue; } return ret_val; }
5、参考:
《C Primer Plus (第6版)中文版》
-
scanf gets
2017-11-14 16:31:08scanf( )函数和gets( )函数都可用于输入字符串,但在功能上有区别。若想从键盘上输入字符串"hi hello",则应该使用__gets__函数。 gets可以接收空格;而scanf遇到空格、回车和Tab键都会认为输入结束,所有它不能... -
VS2015提示gets未定义
2018-09-17 17:16:46出现这种情况的一般原因是, VS2015使用的是新C标准,也就是C11,而VC6.0用的是老标准。 在新标准中,应该是用gets_s代替gets gets_s的具体用法是: gets_s(char *buff,size) ... -
Consistent gets
2019-09-24 07:58:08Consistent gets Revision as of 15:18 2007年4月30日 by Liubin (Talk | 贡献) (差异) ←Older revision | Current revision (差异) | Newer revision→ (差异) #main { border-top: 1px solid #666; ... -
gets和fgets函数及其区别,C语言gets和fgets函数详解
2018-11-30 22:34:05gets和fgets函数及其区别,C语言gets和fgets函数详解 每当讨论 gets 函数时,大家不由自主地就会想起 1988 年的“互联网蠕虫”,它在 UNIX 操作系统的 finger 后台程序中使用一个 gets 调用作为它的攻击方式之一。很... -
Gets和scanf的区别
2018-09-15 15:26:42转载自:Gets和scanf的区别 char s[20]; gets(s); puts(s); gets与scanf输入字符串的方式也非常类似,但是有几个区别: (1) gets的输入分割符只有回车,因此gets是能够读入空格的。如果输入为"hello world"时,... -
Gets函数
2017-11-11 16:13:31Gets函数 1.功能:接收用户从键盘键入的字符串,直到接收到换行符或者EOF为止,将读取结果存放在()中所指向的数组中。2.一般形式: char string[100]; gets(string);//其中string为字符串变量(字符串数组名或... -
gets和scanf
2020-09-25 21:35:23好久没有用gets因为,今天突然遇到一个题输入字符串中字符也要算上,就一直过不去,最后看了一下别人都用gets才想起来,这个东西。 gets与scanf输入字符串的方式也非常类似,但是有几个区别: (1) gets的输入分割符... -
gets_s
2020-09-29 16:40:45char *gets_s(char *buffer,size_t sizeInCharacters); 其中第二参数就是允许的输入长度bai, 这里的size应该分配为dubuffer-1的长度,因为函数自动为’\0’分配空间的时候会溢出。和gets的区别就是gets读入不限制... -
getchar与gets
2019-08-13 01:47:38当gets前面有scanf("")等时,需要用getchar吃掉回车;而gets,也会吃掉回车 所以当gets gets接连使用时,并不需要getchar吃掉回车; -
c语言gets_C语言基础教程gets函数与fgets函数
2020-11-29 05:21:41C语言基础教程--gets()与fgets()在c语言中读取字符串有多种方法,比如scanf() 配合%s使用,但是这种方法只能获取一个单词,即遇到空格等空字符就会返回。如果要读取一行字符串,比如:I love BIT这种情况,scanf()就... -
db block gets (current gets) VS. consistent gets
2013-08-21 01:50:04当前读(db block gets / current gets)与一致读(consistent gets)统称为逻辑读,逻辑读可能需要物理读把块读到cache中。当前读指读现在已提交了的数据,一般在全表扫描读数据字典、更新、删除时发生。一致读指读... -
gets,gets_s,fgets函数
2016-05-06 23:09:00这次就说一下,gets(),gets_s(),fgets(),;;;;【在某一篇博客上看到的】 C的标准库gets函数不对接受字符串的buffer进行边界检测,会造成越界,从而产生bug; fgets函数,数获取的字符串与gets函数获取的不一样,... -
函数gets()的用法
2018-09-24 20:48:43gets()函数和scanf()对比 先看代码: #include&amp;amp;amp;lt;stdio.h&amp;amp;amp;gt; int main(void){ char a1[10], a2[10],a3[10]; scanf( &amp;amp;quot;%s%s&amp;amp;quot;... -
gets和gets.chomp
2013-12-02 16:29:58chomp方法是移除字符串尾部的分离符,例如\n,\r等...而gets默认的分离符是\n 转载于:https://blog.51cto.com/abian/1334974 -
c++ gets函数
2019-10-01 03:41:54函数名称:gets函数 函数结构:gets() 所需头文件:#include<cstdio> 函数作用:持续读入,直到遇到换行停止输出。 转载于:https://www.cnblogs.com/lipeiyi520/p/8439377.html... -
Memcached gets 命令
2018-09-19 17:42:00Memcached gets 命令获取带有 CAS 令牌存 的value(数据值),如果 key 不存在,则返回空。 语法: gets 命令的基本语法格式如下: gets key 多个 key 使用空格隔开,如下: gets key1 key2 key3 参数说明如下: ... -
gets() 实战
2019-02-13 16:01:05gets: gets是用来输入一行字符串的,以“\n”识别输入结束,puts对应一行字符串输出。 用法 char str[100]; gets(str); puts(str); 实战 采用c++编译器提交出现 error: ‘gets’ was not declared in ... -
gets,fgets,gets_s的区别分析
2020-08-07 15:48:45在最开始学习字符串时,我们最常用的输入字符串函数就是——gets(); 这个函数将输入缓冲区中的数据存储到字符型数组中,以换行符结束,并丢弃末尾的换行符添加一个空字符。他解决了scanf("%s",str);以空白符为截至的...
-
【数据分析-随到随学】量化交易策略模型
-
K8S Informer机制分析
-
get.exe中获取四个工具包.zip
-
three.js入门速成
-
Spring Boot 中自定义swagger-ui的URL(path)和样式(UI)
-
jdk-8u141-windows-x64.rar
-
壹伴小插件7.3.3.crx
-
【我给面试官画饼】Python自动化测试面试题精讲
-
Ntttcp 网络测试工具使用方法
-
全国计算机等级考试二级MS Office高级应用课程视频教程-二级office
-
Java 和大数据面试题
-
分布式任务调度平台XXL-JOB
-
全志ANDROID10 在根目录下创建挂载点.
-
flutter插件调用APP页面、使用原生aar,framework库
-
Windchill_对象概要.xmind
-
位 运 算 符
-
电气符号总集大全.zip
-
阿里云云计算ACP考试必备教程
-
面向对象期末考试题.docx
-
使用QGIS生成的武汉路网geojson数据与xml数据