gets和fgets的用法区别 解决warning: the `gets‘ function is dangerous and should not be used.

gets函数的基本用法:一般在window下

fgets()函数的基本用法:一般用在Linux环境下

char *fgets(char *s,int size,FILE *stream);//eg:可以用fgets(tempstr,10,stdin)//tempstr 为char[]变量,10为要输入的字符串长度,stdin为从标准终端输入。

代码示例:

#include <stdio.h>

int main ( ) {
 
   char name[20];
 
   printf("\n 输入任意字符 : ");
 
   fgets(name, 20, stdin);//stdin 意思是键盘输入
 
   fputs(name, stdout); //stdout 输出
 
   return 0;
}

在Linux编译C程序时使用gets,会出现:
warning: the `gets' function is dangerous and should not be used.

问题出在程序中使用了 gets  ,Linux 下gcc编译器不支持这个函数,解决办法是使用 fgets

这个问题主要应该是因为linux 和 windows的文件在换行符上编码不一样导致的,linux的换行是\0,windows的换行是\13\0,是两个字符

gets从终端读入是的字符串是用\0结束的,而fgets是以\n结束的(一般输入都用ENTER结束),然后strcmp两者的时候是不会相等的!

猜你喜欢

转载自blog.csdn.net/m0_74712453/article/details/132641151
今日推荐