ゲームが作れるようになるまでがんばる日記

ゲーム制作のことを中心にゲームに関することを書いています

ファイルの出力

テキストファイルの出力にはofstreamを使えば簡単に行える。
ofstreamのストリームを作成し、あとはコンソールと同じように<<演算子で出力できる。出力し終わったあとはclose()で閉じる。

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

int main()
{
    ofstream outFile( "test.txt" );
    if ( !outFile ) {
        cout << "can't open file." << endl;
        return -1;
    }

    outFile << "This is test." << endl;

    outFile.close();

    return 0;
}