finding a binary pattern in a file.

  • Thread starter Shashank Khanvilkar
  • Start date
S

Shashank Khanvilkar

Hi,
I want to write a simple program that can search for a 13 _BIT_ pattern
in a file opened in a binary mode. Is there a simple way of doing this?
for example, can someone complete the below program code.

open(IN,"$inFile");
binmode(IN);

while (<IN>) //while there is still data in the file
{
Read the IN file for the 13 bit pattern
if found{
..do something
}
}
Thanks
Shashank
 
X

xhoster

Shashank Khanvilkar said:
Hi,
I want to write a simple program that can search for a 13 _BIT_ pattern
in a file opened in a binary mode. Is there a simple way of doing this?
for example, can someone complete the below program code.

Is the file much smaller than memory? I'll assume not...
open(IN,"$inFile");
binmode(IN);

while (<IN>) //while there is still data in the file
{
Read the IN file for the 13 bit pattern
if found{
..do something
}
}
Thanks
Shashank

warn "Untested Code";
my $thing_to_find = some_string_of_ones_and_zeros();
my $count=0;
my $buffer='';
while (<IN>) { #presumably, $/ is set to something appropriate
$buffer .= unpack "B*", $_;
my $i=index $buffer, $thing_to_find;
unless ($i == -1) {
print "Found at ", ($i+$count), "\n";
};
$count+= length ( substr $buffer, 0, -length($thing_to_find),'');
};

Xho
 
N

News KF

Hi,

I think this solution should work.
However $buffer would grow during the parsing and could therefore blow
up the memory.


If the file is big and there is no delimiter, then you should probably
use the read statement in order to read junks of data.

Additionally you had to be sure, that the search pattern is not in the
part, that is wrapping (pattern is half at end of previous junk and half
at beginning of new junk).



the code below should roughly work: (I did not try it, so some errors
may be in):

my $searchspace="";
my $chunksize = 1024; # 1 kB in one chunk
my $hitcount=0;
my $positioninstream=0;

while (!eof(IN)){
read($chunk,$chunksize,$_); #### please check exact order of
### read or sysread. Don't know it by
### heart
$searchspace .= unpack("B*", $_); #
my $i;
do {
$i=index($searchspace,$thing_to_find);
if($i>=0){
print "Found at ", ($i+$positioninstream), "\n";
$positioninstream += $i+length($thing_to_find);
$searchspace =
substr($searchspace,$i+$length($thing_to_find));
}
} while $i>=0;
}


bye


nkf.


P.S. This suggested solution is not optimized for speed thoug.
 

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,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top