Click here to Skip to main content
15,946,342 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm making my own map editor and i'm using fstream to write the file that needs writing.
The problem I have with writing the file is that I want the program to change the numbers inside the file (each time the program opens the file is beeing written to), but when the program is writing to the file it overwrites everything insted of changing or adding.
For example I want the program to change the file like this:
(the existing content in the file):
00 00 00 01

and if something is being written I want it to add it like this:
>01< 00 00 00 01

but what my program is doing right now is overwriting everything like this:
(the existing content in the file):
00 00 00 01

(things are being written to the file):
>01<<br mode="hold" />
So basically I want to know how to add stuff and not overwrite it.
Updated 25-Oct-11 0:15am

You need to append to the file. You are probably doing something wrong. Can you show your file writing code?

With fstream, you most likely need to use ios::app to append to the existing file.
Share this answer
Sergey Alexandrovich Kryukov 24-Feb-11 18:49pm    
What OP does is not appending, but adding at zero position, this is insert.
I agree that the design flaw is very likely.
Nish Nishant 24-Feb-11 18:50pm    
Yeah I realized that only when I saw Espen's response :-)
Assuming you are using a text file - to turn this
00 00 00 01
>01< 00 00 00 01

You first need to move the existing content of the file to make room for the additional 5 characters ">01< ".

You need to start at the end, first moving the last character the required distance, then the next to last, and so on until all the existing text has been moved to make place for the characters you want to insert - now that's the principle. Usually you would use a buffer and move blocks at a time to speed things up.

This isn't worth the effort unless your file is quite big. For files less than - let's say 5 MB - just load everything into memory using a suitable structure, manipulate your data and write it out again.

If you need elaborate parsing take a look at antlr[^]

Espen Harlinn
Share this answer
Nish Nishant 24-Feb-11 17:47pm    
Good response, voted 5. I didn't realize he wanted to insert the data at the beginning of the file.
Espen Harlinn 24-Feb-11 18:07pm    
Thank you Nishant - from his description it's my best guess :)
Sergey Alexandrovich Kryukov 24-Feb-11 18:47pm    
Good, my 5.
Sandeep Mewara 25-Feb-11 4:32am    
Good answer!5!
Espen Harlinn 25-Feb-11 5:02am    
Thank you Sandeep!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900