Overwriting a record in a binary file

Posted on 2008-11-15
Medium Priority
Last Modified: 2012-05-05
Hi, here I am trying to overwrite a record in a binary file, however it overwrites the entire file with the write function.  I have attempted to use ios::trunc in the fstream constructor, but still produces same result.

*in this example code i'm simply trying to overwrite the first record, I can retrieve this.
fstream f("records.bin", ios::binary | ios::out);
f.write(reinterpret_cast<const char*>(&binPerson),sizeof(binPerson));

Open in new window

Question by:pangelll
LVL 45

Accepted Solution

sunnycoder earned 2000 total points
ID: 22966826
IIRC this is issue with file opening mode ... You need to open it in rw mode ... opening in w mode truncates it

fstream f("records.bin", ios::binary | ios::out | ios::in);

Author Closing Comment

ID: 31517071
wow! Thankyou.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
There's never been a better time to become a computer scientist. Employment growth in the field is expected to reach 22% overall by 2020, and if you want to get in on the action, it’s a good idea to think about at least minoring in computer science …
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

616 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question