why g++ shows “gets()” not declared ,even after including

后端 未结 2 1412
我寻月下人不归
我寻月下人不归 2021-01-28 21:33
#include 
#include 
#include 

using namespace std;

int main()
{
    char str[30];
    gets(str);
}

when

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-28 22:11

    gets is an unsafe function and is not supported by the C Standard any more.

    Instead use fgets.

    For example

    #include 
    #include 
    #include 
    
    int main()
    {
        char str[30];
    
        std::fgets(str, sizeof( str ), stdin );
    
        str[ std::strcspn( str, "\n" ) ] = '\0';
    
        //...
    }
    

提交回复
热议问题