C语言的getc()函数和gets()函数的使用对比
C语言的getc()函数和gets()函数的使用对比
发布时间:2016-12-28 来源:查字典编辑
摘要:C语言getc()函数:从流中读取字符头文件:#include函数getc()用于从流中取字符,其原型如下:intgetc(FILE*str...

C语言getc()函数:从流中读取字符

头文件:

#include <stdio.h>

函数getc()用于从流中取字符,其原型如下:

int getc(FILE *stream);

【参数】参数*steam为要从中读取字符的文件流。

【返回值】该函数执行成功后,将返回所读取的字符。

【说明】若从一个文件中读取一个字符,读到文件尾而无数据时便返回EOF。getc()与fgetc()作用相同,但在某些库中getc()为宏定义,而非真正的函数。

【实例】下面的示例演示了getc()函数的使用,在程序中采用该函数从标准输入控制台中读取字符,代码如下。

#include <stdio.h> //引入标准输入输出库 void main( ) { char ch; printf ("Input a character: "); //输入提示信息 ch = getc(stdin); // 从标准输入控制台中读取字符 printf ("The character input was: '%c'n", ch); // 输出字符 }

运行上述程序,首先声明一个用于保存所取字符的变量;然后输 出提示信息,接收从标准输入控制台按下的任意键,并将该字符输出到控制台。

利用getc()从文件中读取字符串,代码如下。

#include<stdio.h> #include<string.h> #include<stdlib.h> int main(void) { int ch; int len; int i=0; FILE* fstream; char msg[100] = "Hello!I have read this file."; fstream=fopen("test.txt","at+"); if(fstream==NULL) { printf("read file test.txt failed!n"); exit(1); } /*getc从文件流中读取字符*/ while( (ch = getc(fstream))!=EOF) { putchar(ch); } putchar('n'); len = strlen(msg); while(len>0)/*循环写入*/ { putc(msg[i],fstream); putchar(msg[i]); len--; i++; } fclose(fstream); return 0; }

函数fopen利用模式“at+”打开文本文件,使用getc从文件流中逐个读取字符,直到读完。

C语言gets()函数:从流中读取字符串

头文件:

#include <stdio.h>

gets()函数用于从缓冲区中读取字符串,其原型如下:

char *gets(char *string);

gets()函数从流中读取字符串,直到出现换行符或读到文件尾为止,最后加上NULL作为字符串结束。所读取的字符串暂存在给定的参数string中。

【返回值】若成功则返回string的指针,否则返回NULL。

注意:由于gets()不检查字符串string的大小,必须遇到换行符或文件结尾才会结束输入,因此容易造成缓存溢出的安全性问题,导致程序崩溃,可以使用fgets()代替。

【实例】请看下面一个简单的例子。

#include <stdio.h> int main(void) { char str[10]; printf("Input a string.n"); gets(str); printf("The string you input is: %s",str); //输出所有的值,注意a }

如果输入123456(长度小于10),则输出结果为:

Input a string. 123456↙ The string you input is:123456

如果输入12345678901234567890(长度大于10),则输出结果为:

Input a string. 12345678901234567890↙ The string you input is:12345678901234567890

同时看到系统提示程序已经崩溃。

如果不能正确使用gets()函数,带来的危害是很大的,就如上面我们看到的,输入字符串的长度大于缓冲区长度时,并没有截断,原样输出了读入的字符串,造成程序崩溃。

考虑到程序安全性和健壮性,建议用fgets()来代替gets()。

如果你在GCC中使用gets(),编译无法通过,会提示:

the 'gets' function is dangerous and shout not be used.

C语言gets()函数:从流中读取字符串

头文件:

#include <stdio.h>

gets()函数用于从缓冲区中读取字符串,其原型如下:

char *gets(char *string);

gets()函数从流中读取字符串,直到出现换行符或读到文件尾为止,最后加上NULL作为字符串结束。所读取的字符串暂存在给定的参数string中。

【返回值】若成功则返回string的指针,否则返回NULL。

注意:由于gets()不检查字符串string的大小,必须遇到换行符或文件结尾才会结束输入,因此容易造成缓存溢出的安全性问题,导致程序崩溃,可以使用fgets()代替。

【实例】请看下面一个简单的例子。

#include <stdio.h> int main(void) { char str[10]; printf("Input a string.n"); gets(str); printf("The string you input is: %s",str); //输出所有的值,注意a }

如果输入123456(长度小于10),则输出结果为:

Input a string. 123456↙ The string you input is:123456 如果输入12345678901234567890(长度大于10),则输出结果为:

Input a string. 12345678901234567890↙ The string you input is:12345678901234567890 同时看到系统提示程序已经崩溃。

如果不能正确使用gets()函数,带来的危害是很大的,就如上面我们看到的,输入字符串的长度大于缓冲区长度时,并没有截断,原样输出了读入的字符串,造成程序崩溃。

考虑到程序安全性和健壮性,建议用fgets()来代替gets()。

如果你在GCC中使用gets(),编译无法通过,会提示:

the 'gets' function is dangerous and shout not be used.

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新C语言学习
热门C语言学习
编程开发子分类