pack /unpack issue

S

sonet

result:
....
<[email protected]> 12345 8
<[email protected]> 12345 9
<[email protected]> 12345 10
<[email protected]> 12345 2816 <==
<[email protected]> 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='<[email protected]>';
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;
 
N

nobull

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 $!;
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top