Reading a file multiple times - file pointers
Archive - Originally posted on "The Horse's Mouth" - 2007-11-23 23:33:01 - Graham EllisWhen you open a file for read, you create a "file pointer" which knows where in the file you are, and each time you read from the file this advances so that you get the next piece of data each time you do a read. Usually you don't see the file pointer at all - it's internal - and you think nothing of it as it behaves in a natural way.
There are, however, times that you want to manipulate the file pointer differently. For example, you may want to re-read the file a number of times from the beginning, or even randomly jump around the file. Closing and re-opening the file will let you start again at the beginning, and most languages offer a functions such as seek which will let you move around with more control.
Here's an example in Perl - I have two files to read and I want to take each line in turn from the first file as a heading, then report on each line in the second file that has the same number of space separated fields in it. Here's my first (incorrect) attempt:
open (FHA,"f1");
open (FHB,"f2");
#
while ($line = <FHA>) {
@fldsin = split(/\s+/,$line);
$count = scalar(@fldsin);
print "\n=======$line";
#
while ($line2 = <FHB>) {
@fldsin2 = split(/\s+/,$line2);
$count2 = scalar(@fldsin2);
print $line2 if ($count == $count2);
}
}
And that's incorrect because it only reads the second file once. If, however, I move my second open statement within the first loop, I'm resetting my file pointer each time and get the results I want:
open (FHA,"f1");
#
while ($line = <FHA>) {
open (FHB,"f2");
@fldsin = split(/\s+/,$line);
$count = scalar(@fldsin);
print "\n=======$line";
#
while ($line2 = <FHB>) {
@fldsin2 = split(/\s+/,$line2);
$count2 = scalar(@fldsin2);
print $line2 if ($count == $count2);
}
}
Here are the results:
earth-wind-and-fire:~/nov07 grahamellis$ perl lls
=======First line
Perl Programming
Ruby Programming
Python Programming
=======This is the second line
=======Third line
=======A fourth line
earth-wind-and-fire:~/nov07 grahamellis$ perl lls2
=======First line
Perl Programming
Ruby Programming
Python Programming
=======This is the second line
Learning to Program in Perl
=======Third line
Perl Programming
Ruby Programming
Python Programming
=======A fourth line
Object Oriented Perl
earth-wind-and-fire:~/nov07 grahamellis$
The purists will point out three things to me about this code ;-)
Firstly, they'll say I should have closed my files each time I had finished with them.
Secondly, they'll say that I should have checked the return status from my file opens to ensure that they really worked
Thirdly, they'll say that I should have read the whole of the second file into a list just once and processed it from there.
For a running program ... they're right, right and usually right ... but for a spike solution / demonstration of the principles of file pointers, I think my example makes a short and elegant example!