• C++ iostream binary reading and writing issues
    32 replies, posted
I think your problem is the insert is not meant to be rerunnable since the insert is the contents for an entire file. You need to handle if a null terminator exists in the file, and if it does, remove it and append another after you insert another THIS. [editline]25th April 2012[/editline] What's basically happening is I'm assuming the tutorial you used is supposed to be like "HEY LOOK IT RAN ONCE PERFECTLY LOOK AT HOW COOL THIS IS." But once the reading stream hits the null terminator it doesn't care what's after that, but if you don't include one you get random gibberish that includes all of the other THIS that's in the file.
I could be mistaken as I have yet to see an example of write() that apparently functions properly with ios::binary and ios::app, but I think the issue lies with ios::app. If I want to append it properly, I will need to overwrite the previous null terminator? The only other solution I can think of is to read in the entire file, make my changes, then overwrite the entire file with the new contents, but this seems somewhat inefficient compared with opening the file and simply appending to it.
[QUOTE=Overv;35714287]By the way, stop using [I]sizeof(char)[/I], it's defined to be 1 by the spec.[/QUOTE] It's simply a good practice to use sizeof(T), and if you eventually refactor with wchar_t then it's even easier to make the switch later on.
Sorry, you need to Log In to post a reply to this thread.