in reply to Question about binary file I/O
The bitwise aspects have been answered already. Re the overall structure of your program, you should use strict and warnings, lexical file handles, and 3-argument open. Also, read is rarely used or needed in Perl programs. Finally, you should set binmode on both files to indicate they are binary files. I suggest something like:
use strict; use warnings; my $infile = 'yourinputfilename'; my $outfile = 'youroutputfilename'; open(my $fhin, '<', $infile) or die "error: open '$infile': $!"; binmode($fhin); open(my $fhout, '>', $outfile) or die "error: open '$outfile': $!"; binmode($fhout); local $/ = \2; # make <> operator read two bytes at a time while (my $TileData = <$fhin>) { if (length($TileData) != 2) { warn "oops, file is of uneven length, last byte='$TileData'\n"; last; } # do your thing with bitwise stuff here... # my $NewTileData = ... print {$fhout} $NewTileData; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Question about binary file I/O
by TheMartianGeek (Acolyte) on Feb 06, 2011 at 02:38 UTC | |
by eyepopslikeamosquito (Archbishop) on Feb 06, 2011 at 04:00 UTC | |
by TheMartianGeek (Acolyte) on Feb 06, 2011 at 16:10 UTC | |
by TheMartianGeek (Acolyte) on Feb 06, 2011 at 06:05 UTC | |
by eyepopslikeamosquito (Archbishop) on Feb 06, 2011 at 07:18 UTC | |
by TheMartianGeek (Acolyte) on Mar 02, 2011 at 05:21 UTC | |
by roboticus (Chancellor) on Mar 02, 2011 at 11:13 UTC | |
by james2vegas (Chaplain) on Feb 06, 2011 at 03:03 UTC |