Trivial C++ program not writing to file
<p>These lines are the sole contents of main():</p>

<pre><code>fstream file = fstream("test.txt", ios_base::in | ios_base::out);
string word;
while (file &gt;&gt; word) { cout &lt;&lt; word + " "; }
file.seekp(ios::beg);
file &lt;&lt; "testing";
file.close();
</code></pre>

<p>The program correctly outputs the contents of the file (which are "the angry dog"), but when I open the file afterwards, it still just says "the angry dog", not "testingry dog" like I would expect it to.</p>

<p>Full program:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;string&gt;
using namespace std;

int main()
{
    fstream file = fstream("test.txt", ios_base::in | ios_base::out);
    string word;
    while (file &gt;&gt; word) { cout &lt;&lt; word + " "; }
    file.seekp(ios::beg);
    file &lt;&lt; "testing";
    file.close();
}
</code></pre>
