?
Solved

Overwriting a record in a binary file

Posted on 2008-11-15
2
Medium Priority
?
894 Views
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.seekp(0);
f.write(reinterpret_cast<const char*>(&binPerson),sizeof(binPerson));
f.close();

Open in new window

0
Comment
Question by:pangelll
2 Comments
 
LVL 45

Accepted Solution

by:
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);
0
 

Author Closing Comment

by:pangelll
ID: 31517071
wow! Thankyou.
0

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