The title pretty much says it all, but still to elaborate:
I would have understood if the language would have restricted me from adding new contents to a file (irrespective of position) because it would lead to fragmentation. But what I do not understand is why it is not possible to:
- Erase contents from the last line, similar to backspacing from EOF. [ASCII/BINARY]
- Erase contents from middle portion of file [ASCII/BINARY]
- Replace text in a file with some other text of same size [ASCII]
- Replace data in a file with some other data of same size [Binary]
Does any other language support this?
EDIT: To do this in C++, you need to read the file, perform the modifications on variables, then create a new file. The question was why it is not possible to edit the "original" file instead of creating another file.
Looks like you were expecting lowish-level access to files to magically work like some word processor / text editor application. But they don't do this, either! They simply abstract away from the user all the complicated mechanisms involved in editing and re-committing a file to disk.
In writing C++ source code, you are now taking responsibility for implementing those mechanisms. It's not quite as simple as dumping a bunch of backspace characters at EOF. :)
C++ supports it. You can seek and you can write. But the fact that this is a very uncommon scenario to replace the same exact byte length and the fact that replacing a bunch of bytes with another bunch of bytes that has a different length is horribly inefficient to a point were writing a new file is faster, lead to the language and functions that exist.
Other languages may handle this differently, but I have yet to learn one that actually does. Because file handling does not change across languages and all language and framework writers came to the same conclusions: files are not the best medium for insertion and deletion of data. Arrays are not the best medium for insertion and deletion, files just being one them.
If you need to insert and delete, use another container (lists come to mind). If you need to persist this container to disk, do so after all manipulation is done.