Problem reading and writing from same file within same program... C++
<p>I'm working on a program, that needs to load data from a text file upon starting and save data to THE SAME text file upon exit. I have the load working, and i have the save working, but for some reason I cant seem to have them both work within the same program.</p>

<p>This doesnt work...</p>

<pre><code>ifstream loadfile("test.txt");
ofstream savefile("test.txt");

void load()
{
string name;

    while(!loadfile.eof())
    {
        getline(loadfile,name);
        cout&lt;&lt;"name " &lt;&lt; name&lt;&lt;"\n";
    }
}

void save(User &amp;name)
{   
savefile &lt;&lt; name.getName() &lt;&lt; endl;
}
</code></pre>

<p>Neither does this...</p>

<pre><code>fstream file("test.txt");

void load()
{
string name;

    while(! file.eof())
    {
        getline(file,name);
        cout&lt;&lt;"name " &lt;&lt; name&lt;&lt;"\n";
    }
}

void save(User &amp;name)
{   
file &lt;&lt; name.getName() &lt;&lt; endl;
}
</code></pre>

<p>The thing is, I can save a list of names, which works fine... but as soon as i start the program, all the names from the list delete from the text file.</p>

<p>Also, I know that getline() gets the data from the text file as a string type, but how would i convert that to something like an int.</p>

<p>Thanks</p>
