When 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!
(written 2007-11-23 23:33:01)
| Commentator | says ... | | anonymous: | Lisa forwarded me an email ...
Lisa forwarded an email to me ... (comment added 2007-12-02 11:51:20) |
| anonymous: | Lisa forwarded me an email ...
Lisa forwarded an email to me ... (comment added 2007-12-02 11:51:20) |
Associated topics are indexed under
H109 - PHP - Input / Output [1780] Server overloading - turns out to be feof in PHP - (2008-09-01)
[1113] File and URL reading in PHP - (2007-03-20)
[1096] Sample script - FTP to get a file from within PHP - (2007-03-01)
[1094] PHP fread - truncated data - (2007-02-27)
[997] Most recent file in a directory - PHP - (2006-12-18)
[709] Handling huge data files in PHP - (2006-05-04)
[653] Easy feed! - (2006-03-21)
[616] printf - a flawed but useful function - (2006-02-22)
[114] Relative or absolute milkman - (2004-11-10)
P207 - Perl - File Handling [2405] But I am reading from a file - no need to prompt (Perl) - (2009-09-14)
[2233] Transforming data in Perl using lists of lists and hashes of hashes - (2009-06-12)
[1861] Reactive (dynamic) formatting in Perl - (2008-10-31)
[1860] Seven new intermediate Perl examples - (2008-10-30)
[1841] Formatting with a leading + / Lua and Perl - (2008-10-15)
[1709] There is more that one way - Perl - (2008-07-14)
[1416] Good, steady, simple example - Perl file handling - (2007-10-30)
[1312] Some one line Perl tips and techniques - (2007-08-21)
[867] Being sure to be positive in Perl - (2006-09-15)
[702] Iterators - expressions tha change each time you call them - (2006-04-27)
[618] Perl - its up to YOU to check your file opened - (2006-02-23)
[255] STDIN, STDOUT, STDERR and DATA - Perl file handles - (2005-03-23)
[12] How many people in a room? - (2004-08-12)
Y110 - Python - File Handling [2282] Checking robots.txt from Python - (2009-07-12)
[2011] Conversion of OSI grid references to Eastings and Northings - (2009-01-28)
[183] The elegance of Python - (2005-01-19)
Some other Articles
An answer to a student asking 'Help'Looking after you Christmas Customer CrowdsUsing English can slow you right down!Christmas is coming very earlyReading a file multiple times - file pointersOn cancellations, rebooking, and pricing schemesUseful command or messy screen?Linux / Unix - layout of operating system filesCopy and paste / cut and paste and other vi techniquesAbove the fold with First Great Western