テキストファイルに何かを書き込む

fstreamをインクルードする。fはfileのf。
ofstream インスタンス名("ファイルパス")ofstreamクラスをインスタンス化する。これで書き込み先のファイルがひらかれる。
ofstreamの"of"は"output file"。

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

int main() {
    ofstream neko("sute.txt");        // 書き込み先のファイルをひらく。

    if (!neko.is_open()) {            // ファイルがひらけなかったら、
        cout << "can not be opened";  // コンソールに何かを表示して、
        return 1;                     // 適当に何かを返してmain()を終了する。
    }

    for (int i = 0; i < 100; i++) {
        neko << i;                    // ファイルに何かを書き込んで、
        neko << ",";
    }

    neko.close();                     // ファイルを閉じて、
    cout << "successfully completed"; // 確認としてコンソールに何かを表示して、
    return 0;                         // main()を終了する。
}

f:id:ti-nspire:20190523072601p:plain