Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Inserting

Name: Anonymous 2009-09-03 7:48

are there any open/write functions for C , which do not overwrite chars in a file , but insert them ?

or do i have to write a shitty loop that just reads , stores and writes a char ?

also discuss file and string handling libraries because most that i have seen suck .

Name: Anonymous 2009-09-03 8:28

>>3
small shit like this is easy to implement but it still should be part of the standard library i think .
It's not as easy as you seem to think. The hard disk can't insert data like that (unless the data comes in, say, 4kB blocks), so crappy manipulation is in fact needed at some point, and this manipulation is quite expensive; and the amount of crappy manipulation needed is proportional not to the amount of data inserted, but the number of steps in which you insert data.

For example, if you insert a block of 10 bytes, the whole file after the insertion point has to be rewritten. If you insert a block of 1 byte 10 times, however, the whole file after the insertion point has to be rewritten 10 times as well.

This fact makes having a standard library here of limited use. In the most cases, the proper way to solve this problem is to read the whole file into memory, do your manipulation work there, and write everything back to disk when you're done. If that's not possible (if the file is too big to keep in memory), it may be best to write a modified copy of the data to a temporary file, and then replace the old file by the new file. (Of course, this has downsides as well.)

also , i assume C++ streams work almost the same ?
Yes, they do, for the same reasons.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List