pack /unpack issue

Discussion in 'Perl Misc' started by sonet, Nov 10, 2005.

  1. sonet

    sonet Guest

    result:
    ....
    <> 12345 8
    <> 12345 9
    <> 12345 10
    <> 12345 2816 <==
    <> 12345 3072
    ....

    if i set $i=11 or any value.

    like:
    $i=11;
    my $packdata=pack("ia30a30", $i ,$mailaddr,$mailtitle);

    the result is ok.
    #---------------------------------------------------------------------------
    ------
    #test1.pl
    #---------------------------------------------------------------------------
    ------
    #!/usr/local/bin/perl

    use strict;
    my $mailaddr='<>';
    my $isread=0;
    my $mailtitle='12345';
    my $serailno=0;

    open FH,">testmdb";
    for (my $i=1;$i<=1000;$i++)
    {
    my $packdata=pack("ia30a30", $i ,$mailaddr,$mailtitle);
    print FH $packdata;
    }
    close FH;

    #---------------------------------------------------------------------------
    ------
    #test2.pl
    #---------------------------------------------------------------------------
    ------
    #!/usr/local/bin/perl
    use strict;
    open (FH,"testmdb");
    my $r=1;

    while(1)
    {
    seek(FH,($r-1)*64,0);
    my $data;
    read(FH,$data,64)==64 or last;
    my($serialno,$mailaddr,$mailtitle)= unpack("ia30a30",$data);
    sleep(1);
    print "$mailaddr\t$mailtitle\t$serialno\n";
    $r++;
    }
    close FH;
     
    sonet, Nov 10, 2005
    #1
    1. Advertising

  2. sonet

    Guest

    sonet wrote:

    [ snip - example code using pack/unpack with files not in binary mode ]

    You should open files in binary mode for binary data.

    > open FH,">testmdb";


    open FH, '>:raw', 'testmdb' or die $!;

    > open (FH,"testmdb");


    open FH, '<:raw', 'testmdb' or die $!;

    Note: on older versions of Perl use the binmode() function.

    Note: on newer versions of Perl consider using lexically scoped
    filehandles rather than global variables.

    open my $fh, '<:raw', 'testmdb' or die $!;
     
    , Nov 10, 2005
    #2
    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. Stacy Mader
    Replies:
    4
    Views:
    838
    Ekkehard Morgenstern
    Nov 22, 2003
  2. tmp123
    Replies:
    5
    Views:
    891
    Tim Roberts
    May 4, 2007
  3. joe shoemaker

    Python pack and unpack question

    joe shoemaker, Jul 15, 2008, in forum: Python
    Replies:
    0
    Views:
    332
    joe shoemaker
    Jul 15, 2008
  4. Kero
    Replies:
    0
    Views:
    219
  5. Bill Atkins
    Replies:
    6
    Views:
    147
    Daniel Berger
    Jun 14, 2004
Loading...

Share This Page