Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > capturing [A-Z]+ in binary file - how? - EMASTER_Z (0/1)

Reply
Thread Tools

capturing [A-Z]+ in binary file - how? - EMASTER_Z (0/1)

 
 
Steve D
Guest
Posts: n/a
 
      01-29-2006
$file_name = 'EMASTER_Z';

if (-B $file_name) {
print "$file_name - is a binary file\n";
}
if (-T $file_name) {
print "$file_name - is a text file\n";
}
open (MASTER_IN, "<$file_name") ||
die "unable to open $file_name file $!";
binmode(STDOUT);
binmode(MASTER_IN);
print "\n----------------- 0 ----------------------\n";
### lets me see the file as it is
print STDOUT <MASTER_IN>;

print "\n----------------- 1 ----------------------\n";
### place data into variable or array
### goal is to capture all text data but no matter what I do nothing
works
### have tried unpack but I do not fully understand it
@master_array = <MASTER_IN>;
chomp @master_array;

$var = pop(@master_array);
print STDOUT $var;
print "--------- done -----------\n";

## what am I doing wrong, or do not understand"
# thanks in advance
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      01-29-2006
Steve D wrote:
>
> open (MASTER_IN, "<$file_name") ||
> die "unable to open $file_name file $!";


<snip>

> ### place data into variable or array
> ### goal is to capture all text data but no matter what I do nothing
> works
> ### have tried unpack but I do not fully understand it


One way:

print map "$_\n", /([[:alpha:]]+)/g while <MASTER_IN>;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      01-29-2006
>>>>> "SD" == Steve D <> writes:

SD> open (MASTER_IN, "<$file_name") ||
SD> die "unable to open $file_name file $!";
SD> binmode(STDOUT);
SD> binmode(MASTER_IN);

SD> print STDOUT <MASTER_IN>;

that will read in all of the file and print it.

SD> @master_array = <MASTER_IN>;

what do you think is happening with that line? you have already read in
the file. handles can't figure out where you want to read from. you have
to open the file again or seek to the beginning to read it all in.

better yet, why do you want to read it all in twice? just read it into
an array and then print it or mung it. you have it backwards, printing
directly from the handle and then trying to read it in again.

and assuming a file is binary and then reading it in as lines makes
little sense. lines are normally found in text files. binary files can
have newlines in them but no guaratees of where and how many. i think
you need to rethink your whole solution.

uri

--
Uri Guttman ------ -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      01-30-2006
Steve D wrote:
> print STDOUT <MASTER_IN>;
>
> @master_array = <MASTER_IN>;
>
> ## what am I doing wrong, or do not understand"


The thing that you do not understand is that <> in list context
will read in the _entire_ file all at once, and print() supplies
list context.

print STDOUT <MASTER_IN>; # Read and write entire file
@master_array = <MASTER_IN>; # Reads nothing, file at EOF already

Binary files may or may not have any linefeeds, therefore you should
read in fixed-length records.

my $record_size = 8192;
open my $in,'<',$file_name or die "Cannot read $file_name: $!\n";
$/ = \$record_size;
while (<$in>) { 'do something with $_'; }

-Joe
 
Reply With Quote
 
Steve D
Guest
Posts: n/a
 
      01-31-2006
On Sun, 29 Jan 2006 19:21:42 -0800, Joe Smith <> wrote:

>Steve D wrote:
>> print STDOUT <MASTER_IN>;
>>
>> @master_array = <MASTER_IN>;
>>
>> ## what am I doing wrong, or do not understand"

>
>The thing that you do not understand is that <> in list context
>will read in the _entire_ file all at once, and print() supplies
>list context.
>
> print STDOUT <MASTER_IN>; # Read and write entire file
> @master_array = <MASTER_IN>; # Reads nothing, file at EOF already
>
>Binary files may or may not have any linefeeds, therefore you should
>read in fixed-length records.
>
> my $record_size = 8192;
> open my $in,'<',$file_name or die "Cannot read $file_name: $!\n";
> $/ = \$record_size;
> while (<$in>) { 'do something with $_'; }
>
> -Joe



Thank you, this helps.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Program to open a file in binary, skip X bytes and write the rest ofthe file to a new file scad C++ 4 05-28-2009 08:47 AM
(8-bit binary to two digit bcd) or (8-bit binary to two digit seven segment) Fangs VHDL 3 10-26-2008 06:41 AM
writing binary file (ios::binary) Ron Eggler C++ 9 04-28-2008 08:20 AM
Newbie: working with binary files/extract png from a binary file Jim Ruby 5 02-01-2008 11:21 AM
Re: ostreams, ios::binary, endian, mixed binary-ascii Marc Schellens C++ 8 07-15-2003 12:27 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57