C++ 基礎

標準入力と出力

以下を事項すると、標準入力を求められます。 1 2 3のような形で入力すると、それぞれ、a,b,cに代入されます。

int main(void)
{
    int a, b, c;
    cin >> a >> b >>  c ;         //標準入力
    cout << a << b << c << endl; //標準出力

    return 0;
}

ファイルを1行ずつ読み込みたいとき

#include<iostream>
#include<fstream>
using namespace std;

int main(void){
    //ファイル読み込み
    ifstream ifs("test.java");
    string str;

    if(ifs.fail()){
        cerr << "failed to open";
        return -1;
    }
    //1行ずつifsから読み込み、strに保存する。
    while(getline(ifs, str)){
        cout << "#" << str << endl;
    }

    return 0;
}