in reply to Looping through a binary file

Suspect your problem is that while(<perlIN>) is causing it to read the whole binary file, all at once, right there. Boom -- out of memory.

Instead, as noted by others, read only what you need -- one record at a time.

A common approach is to use a flag variable to indicate when you reach end of file; control the loop based on that.

Two ways to do this:

  1. Read one record at the top of the loop (use read and your $recln value); use substr to extract the pieces, or;
  2. Don't read anything at the top of the loop, but instead, read each variable like you do currently do; modify each one to check for end of file (sample below).

my $TRUE = 1; my $FALSE = 0; [...] my $EofFlag = $FALSE; while(!$EofFlag) { [...] my $cmtseq_cnt = read(perlIN,$cmtseq,5); if ($cmtseq_cnt < 5) { $EofFlag = $TRUE; # Redundant, given what we do next, but h +ere for example last; } }