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

Discussion in 'Perl Misc' started by Steve D, Jan 29, 2006.

  1. Steve D

    Steve D Guest

    $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
     
    Steve D, Jan 29, 2006
    #1
    1. Advertising

  2. 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
     
    Gunnar Hjalmarsson, Jan 29, 2006
    #2
    1. Advertising

  3. Steve D

    Uri Guttman Guest

    >>>>> "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
     
    Uri Guttman, Jan 29, 2006
    #3
  4. Steve D

    Joe Smith Guest

    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
     
    Joe Smith, Jan 30, 2006
    #4
  5. Steve D

    Steve D Guest

    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.
     
    Steve D, Jan 31, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. NETRavi via DotNetMonster.com

    ASP.NET : Capturing a portion of a web page into a jpeg file

    NETRavi via DotNetMonster.com, Nov 17, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    334
    NETRavi via DotNetMonster.com
    Nov 17, 2005
  2. Gerald
    Replies:
    1
    Views:
    699
    Roedy Green
    May 9, 2004
  3. sunita

    capturing from file download dialog

    sunita, Feb 7, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    352
    sunita
    Feb 7, 2007
  4. Ron Eggler

    writing binary file (ios::binary)

    Ron Eggler, Apr 25, 2008, in forum: C++
    Replies:
    9
    Views:
    935
    James Kanze
    Apr 28, 2008
  5. Jim
    Replies:
    6
    Views:
    736
Loading...

Share This Page