1. File editing

I was wondering if it was possible to add to the beginning of a file(prepend
to a file) without having to copy the contents, add the new stuff then put the
old stuff back in. Any help would be appreciated!

new topic     » topic index » view message » categorize

2. Re: File editing

ThatNerd at AOL.COM wrote:
>
> I was wondering if it was possible to add to the beginning of a file(prepend
> to a file) without having to copy the contents, add the new stuff then put the
> old stuff back in. Any help would be appreciated!

If you mean adding to the beginning of a physical disk file - no
feasable way.
Where would your write the bytes, if for example your file was the first
one
on a disk? You gotta move some data to make room, no matter what
language you're
using.

You have to do something like:

fn = open("filename.xxx","u") -- open for update
oldsequence = get(fn)
if oldsequence[1] = GET_SUCCESS then
   oldsequence = oldsequence[2] -- remove the success flag
   newseq = prepend(oldsequence,newstuff) -- add new stuff
   seek(fn,0) -- reset to beginning of file
   print(fn,newseq)
end if

What if you have a *really big file* and it takes too long to read and
write all that
data? You tack new stuff onto the end using the open file for append
function, and then reindex the records. Many databases handle the
problem in that way; record 1 may be
actually the 32,112 th record on the physical file, record 2 may be the
fifty-third.

Regards,

Irv

new topic     » goto parent     » topic index » view message » categorize

3. Re: File editing

I think a possible solution to prepend sequence A to file B might be to do
this :
Open file B (for binary append) and append to it (rubbish) bytes of length
A. Close B.
Open B( for binary update(?)) and move the original sequence to the end of
the file.
I suppose that the program structure to do the last part would be like this
(in quasi-code):

for pointer= length file B to 1 by -1
get byte B[pointer]
write byte to B[pointer + length A]
end for

Then...
Add sequence A to the front of file B, ie:

for pointer= 1 to length A
get byte A[pointer]
write byte to B[pointer]
end for

Now, this is a very rough answer but I think it has merit because you will
not need to load into Eu anything longer than the sequence A. Also the
above quasi-code logic could be optimized somewhat by reading and writing
chunks of data rather than single bytes.



Yours Truly
Michael Palffy

michael at igrin.co.nz
----------
> From: ThatNerd at AOL.COM
> To: EUPHORIA at cwisserver1.mcs.muohio.edu
> Subject: File editing
> Date: Friday, 24 July 1998 12:08
>
> I was wondering if it was possible to add to the beginning of a
file(prepend
> to a file) without having to copy the contents, add the new stuff then
put the
> old stuff back in. Any help would be appreciated!

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu