#!/usr/bin/perl
print "Content-type: text/html\n\n";
#List14_09
#Read fixed length records from a file
#1 Verify the record length
$NL = "\n";
$DC = sprintf("%c", 236); #Special character
$TW = 63; #Line with
$DL = sprintf( "%63s", $NL . $DC x $TW); #top and bottom lines
$Record = "A3 A20 A20 A20";
$RecordLength = length(pack($Record));
print "$NL Record length: $RecordLength";
#2 Open the data file--read only
open(FILE, "lab8_tcpdump2.txt") || die "\n Cannot access file.";
#3 Set the file at the end (option 2)
seek(FILE, 0 , 2);
# Obtain the length
$FileLength = tell(FILE);
print "$NL The length of the file is $FileLength";
$NumOfRecords = int($FileLength/$RecordLength);
print("$NL $NumOfRecords records are in the file.");
print($DL);
print "$NL>>Press ";
#4 Use seek and read to obtain records by multiplying by length
for($Itr = 0; $Itr < $NumOfRecords; $Itr++){
$Count = $Itr + 1;
$AnyChar = ;
seek(FILE, $Itr * $RecordLength, 0);
read(FILE, $LineRead, $RecordLength, 0);
print(" >> $Count $LineRead");
}
print($DL);
#5 Use seek and record length to find a record for display
seek(FILE, 0, 0);
print("$NL $NL Enter a record number. $NL >>");
$LineNumber = ;
# print "$NL The record length: $RecordLength $NL";
$SeekPoint = ($LineNumber - 1)* $RecordLength;
seek(FILE, $SeekPoint, 0);
read(FILE, $LineRead, $RecordLength);
# print "$NL The line read is: $NL";
print "$LineRead\n";
close(FILE) || "Cannot close file: $!";
print "\n";
print "\n";
<>