这些行是 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();
}