Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > pack /unpack issue

Reply
Thread Tools

pack /unpack issue

 
 
sonet
Guest
Posts: n/a
 
      11-10-2005
result:
....
<(E-Mail Removed)> 12345 8
<(E-Mail Removed)> 12345 9
<(E-Mail Removed)> 12345 10
<(E-Mail Removed)> 12345 2816 <==
<(E-Mail Removed)> 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='<(E-Mail Removed)>';
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;


 
Reply With Quote
 
 
 
 
nobull@mail.com
Guest
Posts: n/a
 
      11-10-2005
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 $!;

 
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
Service Pack 2: login issue's and power management issue's ?!? Skybuck Flying Windows 64bit 0 04-07-2007 03:12 PM
Buy 1 pack or 3 pack? (XP Pro x64) Rich Olver Windows 64bit 11 11-25-2006 11:33 PM
Excellent discount software packs - ImTOO Ripper Pack Platinum and ImTOO Ripper Pack Gold zhangelf01@gmail.com DVD Video 6 09-17-2006 03:27 AM
Pack parent control in child using control.pack(in_= syntax? Tim Jones Python 0 01-31-2004 10:22 PM
Poor Mans NIMH Battery Pack and SLA Battery Pack ajacobs2 Digital Photography 0 08-19-2003 12:42 PM



Advertisments