C++ 中的gets函数哪里去了?为何编译报错?

根据 https://zh.cppreference.com/w/cpp/io/c/gets , gets函数已经被移除。 可以使用#define gets(S) fgets(S,sizeof(S),stdin) 作为兼容性宏替换。

示例1

#include <iostream>
using namespace std;
int main(){
    char a[200];
    fgets(a,sizeof(a),stdin);
    cout<<a<<endl;
    return 0;
}

示例2

#include <iostream>
using namespace std;
#define gets(S) fgets(S,sizeof(S),stdin) 
int main(){
    char a[200];
    gets(a);
    cout<<a<<endl;
    return 0;
}

注意:fgets输入会输入回车符

发表评论