Re: Question with routine...

new topic     » topic index » view thread      » older message » newer message

Leviathan,

You could try to insert 'end-of-file' character at the appropriate
place, but the method below is cleaner, more reliable and also faster.
It uses Euphoria's system() command. It copies the input file into a
temporary file, rewrites the modified original, and if every thing
goes ok, it deletes the temporary file. jiri

--  del.ex
--  jbabor at paradise.net.nz
--  01-01-14

include file.e

procedure make(sequence name)
    -- create a test file

    integer f

    f = open(name, "wb")
    if f = -1 then
        puts(1, "Error: Couldn't open " & name & " file!\n")
        abort(1)
    end if
    for i = 0 to 1023 do
        puts(f, i)
    end for
    close(f)
end procedure

procedure del_bytes(sequence path, integer offset, integer bytes)
    integer c,f1,f2,n

    system("copy " & path & " temp", 2)
    f1 = open("temp", "rb")
    if f1 = -1 then
        puts(1, "Error: Couldn't open temp input file!\n")
        abort(1)
    end if
    if seek(f1, -1) then
        puts(1, "Error: Couldn't reach end of temp input file!\n")
        close(f1)                   -- close temporary file
        system("del temp", 2)       -- delete temporary file
        abort(1)
    end if
    n = where(f1)                   -- length of input file
    if n < offset+bytes-1 then
        puts(1, "Error: input file too short!\n")
        system("del temp", 2)       -- delete temporary file
        abort(1)
    end if
    c = seek(f1, 0)                 -- return back to start of file

    f2 = open(path, "wb")
    if f2 = -1 then
        close(f1)                   -- close input file
        puts(1, "Error: Couldn't open output file!\n")
        system("del temp", 2)       -- delete temporary file
        abort(1)
    end if
    for i = 1 to offset-1 do
        puts(f2, getc(f1))          -- copy bytes below offset
    end for
    for i = 1 to bytes do
        c = getc(f1)                -- dump
    end for
    for i = 1 to n-offset-bytes+1 do
        puts(f2, getc(f1))          -- copy tail bytes
    end for
    close(f2)
    close(f1)
    system("del temp", 2)           -- delete temporary file
end procedure

-- test --------------------------------------------------------------
make("data")
del_bytes("data", 257, 16)

----- Original Message -----
From: "LEVIATHAN" <leviathan at USWEST.NET>
To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
Sent: Saturday, January 13, 2001 7:33 PM
Subject: Question with routine...


> Heya all!
>
> I almost have this delete thing working right, except for one
thing - it
> doesn't clean up the bytes at the end of the file. How would I do
this?
> (And like the routine, I need to keep the memory usage extremely
> low)
>
> TIA,
>
> --"LEVIATHAN"

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu