ファイルの読み書きの構文とニュアンスを学んでいます。これが私の問題です。コードがユーザー フラグ (write_outfile = true) に基づいてファイルに書き込む場合、最後にファイルを閉じようとすると、「未定義の識別子」エラーが発生します。
ただし、同じ「if」ステートメント内でファイルを開いてから閉じると、問題はありません。
面倒なコード スニペットを次に示します。
#include <iostream>
#include <fstream>
int main()
bool write_outfile = true;
if (write_outfile)
{
ofstream outfile;
outfile.open("output_test.txt");
outfile << "This is my first text file written from C++.\n";
}
// Do some other stuff here
if (write_outfile)
{
outfile.close();
}