cpp输入

1.cin

适合不存在空格,或者有限个元素的组合

int count;
cin >> count;
int a[count]={0};
for(int i = 0; i < cout; ++i) cin >> a[i];

2.getline

适合一下子就进来一整行的情况

string str1;

getline(cin, str1);

 

 

3.getchar 

循环获取字符

int c;
while(c = getchar());

 

 4.getLine

或者像python的input一样

string name = getLine(“Type your name: “);


5.读入文件 

如果读入失败,不会导致程序崩溃

可以使用getline整行读入,也可以使用input>>变量,读入单个单词

#include <fstream>
int main(){
    ifstream input;
    input.open(“poem.txt”);
    string line;
    while(getline(input, line)){
        cout << line << endl;
    }
    input.close();
}

 

posted on 2022-03-15 15:05  MIXTAPE_208  阅读(87)  评论(0)    收藏  举报

导航