How can I add some chars to a the beginning of an existing file without overwriting (and without copying the entire file and creating a new one!)?
Thanks!
How can I add some chars to a the beginning of an existing file without overwriting (and without copying the entire file and creating a new one!)?
Thanks!
you dont.
.........
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.
You mean "can't"!
Yes, he means cannot. For practical purposes this is un-possible This is nothing to do with Qt. Rather, it is a basic limitation of the high-level concept of a sequential computer file.
With random access to the file you could try to fake it by:
- extending the file by n bytes,
- copying each successive block (starting at the end) along by n bytes to free n bytes at the front, then
- put n extra bytes at the front of the file.
You don't save on I/O and it isn't efficient (you copy the entire file and seek a lot), but you don't need two copies of the file to exist side-by-side.
I expect a different solution to your unstated problem is a better choice.
Ouch That's a beautiful case of ignoring things like caches.
I'm guessing rewriting the whole file would be way faster. One can read an existing block of data, store it in memory (or in a separate file), write new content, read another block, write the previously saved block, etc. And of course one needs to keep his fingers crossed that no power failure takes place.
Right
If data integrity is important the only safe way is to write into a second file and then rename it to the orignal name. On good operating systems the rename is an atomic operation, so there is either the old file or the new file.
Someone at KDE is working on a class for Qt5 that does that internally (Window's rename isn't atomic, the class needs some other code path there).
Cheers,
_
Bookmarks