警告:`gets' 函数很危险,不应使用

warning: the `gets' function is dangerous and should not be used

嗨,当我在终端中输入 make 时,我收到了这条消息。 gets有什么问题?我必须改变什么吗?感谢您的帮助。

user@ubuntu:~/Desktop/Project$ make
gcc -g -ansi -pedantic -Wall -lm project.o -o project 
project.o: In function `main':
project.c:(.text+0x2c8c): warning: the `gets' function is dangerous and should not be used.
void main(){
    char File_Name[55] = { "[=11=]" };
    printf("Give Me File Name:\n");
    gets(File_Name);
    strcat(File_Name, ".as");
    Read_From_File(File_Name);
    printf("\n*******************************************\n");
    free_malloc();
}

get函数不安全,C标准不支持。使用的数组可以被覆盖超出其大小。而是使用函数 fgets。那就是代替这个语句

gets(File_Name);

至少写个赞

fgets( File_Name, sizeof( File_Name ), stdin );

函数可以在输入的字符串后附加换行符'\n'。要删除它,请使用以下代码

#include <string.h>

//...

fgets( File_Name, sizeof( File_Name ), stdin );

File_Name[ strcspn( File_Name, "\n" ) ] = '[=12=]';

考虑到这个初始化

char File_Name[55] = { "[=13=]" };

相当于

char File_Name[55] = "";

或到

char File_Name[55] = { '[=15=]' };