1. Truncate files

Hiya,

This may sound like an obvious question: how do you truncate a file to a
specified size?

I haven't been able to do it using purely Euphoria code with no platform
dependencies; Google throws up a few results from Windows/*nix, so I'm presuming
it's OS specific thing? or am I missing something.. if anyone could shed some
light on any way to do it using only the Euphoria core functions I'll buy you a
beer

new topic     » topic index » view message » categorize

2. Re: Truncate files

FD(censored) wrote:
> 
> Hiya,
> 
> This may sound like an obvious question: how do you truncate a file to a
> specified
> size?
> 
> I haven't been able to do it using purely Euphoria code with no platform
> dependencies;
> Google throws up a few results from Windows/*nix, so I'm presuming it's OS
> specific
> thing? or am I missing something.. if anyone could shed some light on any way
> to do it using only the Euphoria core functions I'll buy you a beer

Does this work as you'd expect?
include get.e
integer fh

-- get the part of the file we want to keep
fh=open(my_filename,"rb")
if fh=-1 then
    abort(1)
end if
-- truncated contents, no padding
constant bytes=get_bytes(fh,requested size) 
close(fh)
-- now clobber file with truncated contents
fh=open(my_filename,"wb")
if fh=-1 then
    abort(1)
end if
puts(fh,bytes)
close(fh)


There may be quicker, not memory intensive, filesystem-specific ways, like
Ctrl-Z for text files on FAT-xx M$ systems. You can also think about using
int #13 to cut the cluster chain short in the FAT. Good luck. The funniest
part is dealing with any of FAT12, FAT16 and FAT32 flavors.

There is an obvious overhead in reading bytes into a sequence and back.
Bypassing this step will require OS specific calls. I could write asm code 
using DOS interrupts to do the job; it will fail wherever they are not
supported.

But at least the above code is pure Eu and platform agnostic.

CChris

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

Search



Quick Links

User menu

Not signed in.

Misc Menu