in reply to How to eliminate text portion of a binary file?

Since the file is binary, you don't want to be reading it with angle brackets (<>) because you don't know if there are ANY new line characters in the part of the file after the headers. Use sysread, and keep adding to the buffer, until you see the end of headers marker. Then just remove the headers with a regex, and cycle through syswrite and sysread (with the appropriate sized buffer, like 16 or 64 K) until you reach the end of file.

Update: here is the example code:

my $buf=""; #append to buffer until headers are found and removed while (!($buf=~s/^.*?#### end_ascii_header\n//)) { sysread(INP,$buf,1024,length($buf)); } syswrite(OUT,$buf,$length($buf)); #copy the rest of the file while (sysread(INP,$buf,16384)) { syswrite(OUT,$buf,length($buf)); }
P.s. In production code, checking the return values of syswrite would be prudent.

Replies are listed 'Best First'.
Re: Re: How to eliminate text portion of a binary file?
by Fletch (Bishop) on May 15, 2004 at 03:08 UTC
    ... you don't want to be reading it with angle brackets (<>) because you don't know if there are ANY new line characters in the part of the file after the headers

    Unless of course you set $/ to a reference to a scalar value containing the number of bytes you want to read each time, in which case it'll work just fine and return that many bytes each read newlines or no.

Re: Re: How to eliminate text portion of a binary file?
by Anonymous Monk on May 14, 2004 at 21:10 UTC
    Hi,

    Ahhh...can you show that in an example? I would appreciate it very much!

    Thanks,

    -fiddler42