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
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.
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.