これらの行は、main() の唯一の内容です:
fstream file = fstream("test.txt", ios_base::in | ios_base::out);
string word;
while (file >> word) { cout << word + " "; }
file.seekp(ios::beg);
file << "testing";
file.close();
プログラムはファイルの内容 (「怒っている犬」) を正しく出力しますが、後でファイルを開くと、期待どおりの「テスト用の犬」ではなく、「怒っている犬」と表示されます。
完全なプログラム:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
fstream file = fstream("test.txt", ios_base::in | ios_base::out);
string word;
while (file >> word) { cout << word + " "; }
file.seekp(ios::beg);
file << "testing";
file.close();
}