外部ファイルを読み込む (C++)
C++
Published: 2020-04-12

確認環境

$ g++ --version
g++ (Homebrew GCC 9.2.0) 9.2.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

調査

sample.txt

aaa
bbb
222
ccc

test.cpp

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

const ll INF = 1LL << 60;

int main() {
    ifstream ifs("sample.txt");
    if (!ifs) {
        cout << "Miss" << endl;
    } else {
        // getを使って読み込むパターン
        // char c;
        // while (ifs.get(c)) {
        //     cout << c;
        // }

        // getline を使って読み込むパターン
        char s[100];
        while (ifs.getline(s, 100)) {
            cout << s << endl;
        }
    }
}

出力結果

aaa
bbb
222
ccc

参考