Re: opening and writing files...
- Posted by Graeme <graemeburke at CROSSWINDS.NET> Jan 12, 2001
- 426 views
At 04:25 AM 11/01/01 -0800, you wrote: >Heya all! > >Me again, this time i'm wondering this: When I open a file for write, >it gets set to 0, I open a file for append, it starts at EOF, I open a file >for update, it lets me seek around, but when I write bytes to the file >at specific points, instead of adding the characters to the file, it >overwrites whatever is there. > >How can I get it so that I can seek to a part of the file, and add >characters? Again, I don't have these files in memory (trying to >make the footprint during usage as tiny as possible) > >TIA, > >--"LEVIATHAN" > > Here's one that moves the end of the file and inserts the data at the specified offset. It does it one byte at a time for low mem usage (as requested), so it's going to be tragicly slow if you want to insert data at the start of a big file. Anyway, here it is. Graeme. --UNTESTED include file.e procedure insert_data(sequence path, integer offset, sequence data) --insert data n at offset integer fn, gap,c gap=length(data) fn=open(path,"ub") if seek(fn,-1) then end if -- goto EOF -- move end of file for x=where(fn)+gap-1 to offset+gap by -1 do if seek(fn,x-gap) then end if c=getc(fn) if seek(fn,x) then end if puts(fn,c) end for --write data if seek(fn,offset) then end if puts(fn,data) close(fn) end procedure ----------------------------------------------------