Scott Haneda wrote:
> Hello, I have this small C app that someone wrote for me ages ago, and
> am tired of it breaking every time I move it around to various
> systems, I will include it below.
>
> I am hoping I can do this in perl, as a one liner, were it will read a
> the first 4 bytes of a file, grab 4 bytes out of the file, which is a
> big endian value, and return that as an int to me.
>
> Here is the basic C app I have been using:
>
> #include <stdio.h>
> #include <libkern/OSByteOrder.h>
>
> int main (int argc, char *argv[]) {
> FILE* fd;
> char buffer[8];
> unsigned long int dv;
>
> //open the file
> fd = fopen(argv[1], "r");
> printf("Filename: %-50s", argv[1]);
>
> fread(buffer, 1, sizeof(buffer), fd);
> dv = OSReadBigInt32(buffer, 4);
>
> // print the decimal value
> printf(" Value: %ld\n", dv);
>
> return 0;
> }
How about this?
Rob
use strict;
use warnings;
my $file = shift;
open my $fh, '<', $file or die $!;
print "Filename: $file";
my $buffer;
my $count = read $fh, $buffer, 4 or die $!;
die "Insufficient data in file" unless $count >= 4;
my $dv = unpack 'N', $buffer;
print " Value: $dv\n";
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/