long
c++ fstream クラスを使用して、テキスト ファイルに aを書き込もうとしています。ファイルは実行前にすでにディスク上に作成されています。次のコードを実行すると、初期値を読み取ることができますが、新しい値を保存できず、上書きされます。私は何を間違っていますか?
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char* argv[]) {
long f;
fstream myFile("data.txt", fstream::in|fstream::out);
cout << "f before: " << f << endl;
myFile >> f;
cout << "f after: " << f << endl;
f++;
cout << "f after increment: " << f << endl;
myFile << f;
myFile.close();
return 0;
}
その後、ファイルの値を読み取りましたが、変更されていません。ここで何が間違っていましたか?