in reply to Re: Mixed text and binary file
in thread Mixed text and binary file

Thanks! Based on your feedback this works. It never ceases to amaze me what perl can do! I'm first preserving the ASCII portion of the file other purposes.

#!/usr/local/bin/perl use strict; use warnings; my $raw_file="spectrebin.raw"; open (FILE,$raw_file); foreach my $line (<FILE>) { if ($line=~/^Binary:/) { #Do not read the binary part as ascii printf "ASCII: $line"; last; } else { #the ascii part printf "ASCII: $line"; } } close(FILE); open (FILE,$raw_file); binmode(FILE) or die "ERROR> Could not read binary file"; $/ = "Binary:\n"; my $text = <FILE>; my $buffer; while ((read (FILE, $buffer, 64))!=0) { my $binary = unpack("B64",$buffer); printf "Binary: $binary\n"; } close(FILE);

Replies are listed 'Best First'.
Re^3: Mixed text and binary file
by Anonymous Monk on Feb 20, 2014 at 21:59 UTC
    It never will cease to amaze you ... what Perl can do ... This language has been called a Swiss-Army Knife, and it deserves the accolade.