Append & delete text in the middle of a file
Daniel Keep
daniel.keep+lists at gmail.com
Sun Jan 14 00:07:09 PST 2007
Heinz wrote:
> == Quote from Heinz (billgates at microsoft.com)'s article
>
>>Hi,
>>I'm trying to replace a value that's in the middle of a text file (using class
>>File: std.stream.Stream). I'm able to set the cursor position to the beginning
>>of the value but in order to replace it i must delete it and append text,
>>there's no way or members to delete chars with the stream class neither append
>>text (except at eof). The provided write operations replace the value but it
>>also replaces other text because the data to write is longer than the original
>>value.
>>Does anybody have an idea to help me?
>>Thanx guys.
>
>
> By 'append' i mean 'insert'. Anyway you can't insert text just like that into the
> middle of a file so i'll use a slice stream from the end of the value to the end
> of file then i'll append this slice in the end of the written value.
To be honest, I can't really think of any way to just insert data into
the middle of a file. Ages ago, when I last did something like this,
the way I did it was to create a second "output" file. I would read in
records one at a time, and stream them out to the output file, which
allowed me to do inserts, deletes and modifications on the fly. At the
end, you can either use the filesystem to delete the old file and move
the new one into its' place or just copy the data from the new output
file to the old input file.
One final thought: if the format your data is in isn't hugely important,
you might want to consider playing with Sqlite. That way, you don't
have to worry about how to do things like inserts or deletes.
Hope this is of some help, and sorry I couldn't solve your problem
directly :)
-- Daniel
More information about the Digitalmars-d-learn
mailing list