how can fstream not overwite filedata?

does anybody know how can i insert data to a file in between the previous data??
eg if a file content is :
"123456789"
and i want to insert "abc" between 4 and 5 so that the file content will be
"1234abc56789"
without having to read the rest of the file,save it somewhere else and then re-insert it

i am trying to construct a b+ tree and this is really important

thank you in advance
Last edited on
Think of a file as an array of char.
For example
 
char mydata[ 50 ] = { 1, 3, 5, 9, 11, 13 };

How do you put a 7 in there between 9 and 11?

Unless your file is particularly huge, it is often easier just to load the whole thing into memory, make your changes, then write it back out.

I hope this gives you some ideas.
Another way is to use a temporary file.
You want to insert y bytes at offset x:
Copy bytes 0 to x-1 from file f to a new file f2. Write the y bytes to file f2. Copy bytes x to n-1 from file f to f2. Delete f. Rename f2 to f.
thanx but loading it to memory or working with 2 file streams is something i want to avoid.Any other ideas are welcome :)
Insertion of data into the middle of a file is not a typical operation supported by a filesystem. Said another way, I think you are out
of luck. You'll probably need to do as helios said.
You can do it without the temporary file. Remember, think of your file as if it were an array. As a hint, treat each element of the array as a 'string' of length <number of bytes to insert>. You'll need a couple of look-ahead buffers and random access on the file.

Hope this helps.
Topic archived. No new replies allowed.