Overwriting a field in a binary file
<p>I'm writing a program that writes structs to a binary file, and then gives the user the option to edit the file. The program should then rewrite that section in the file where the original struct was written. Code:</p>

<pre><code>struct Record
{
    char name     [16];
    char phoneNum [16];
    float balance;
};

int edit     ( fstream&amp; ref)
{
    char searchVal[16];
    cout &lt;&lt; "Enter customer name: ";
    cin.ignore();
    cin.getline(searchVal, sizeof(searchVal));
    int position = -1;
    Record buffer;
    bool found = false;

    while(!ref.eof() &amp;&amp; !found)
    {
        position = ref.tellg();
        ref.read(reinterpret_cast&lt;char*&gt;(&amp;buffer), RECORD_SIZE);
        if((strcmp(buffer.name,searchVal) == 0))
        {
            found = true;

            cout &lt;&lt; buffer.name &lt;&lt; " found! " &lt;&lt; endl;

            cout &lt;&lt; "Enter new customer name: ";
            cin.getline(buffer.name, sizeof(buffer.name));
            cout &lt;&lt; "Enter new customer phone number: ";
            cin.getline(buffer.phoneNum, sizeof(buffer.phoneNum));
            cout &lt;&lt; "Enter new customer balance: ";
            cin &gt;&gt; buffer.balance;

            ref.seekg(-(RECORD_SIZE), ios::cur);
            ref.write(reinterpret_cast&lt;char*&gt;(&amp;buffer), RECORD_SIZE);

            position = ref.tellp();
            break;
        }
    }   
    if(!found)
    {
        cout &lt;&lt; "Record not found" &lt;&lt; endl;
    }

    ref.clear();
    ref.seekg(0L, ios::beg);
    return position;
}
</code></pre>

<p>BAsically, the record is found and the user can "edit" it, but it is written at the end of the file, and I'm not sure why. I appreciate your help on this.</p>
