Home Accessibility Courses Diary The Mouth Facebook Resources Site Map About Us Contact
Reading a file multiple times - file pointers

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, updated 2007-11-27)

Commentatorsays ...
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 as below, or enter http://melksh.am/nnnn for individual articles
H109 - PHP - Input / Output
  [3424] Divide 10000 by 17. Do you get 588.235294117647, 588.24 or 588? - Ruby and PHP - (2011-09-08)
  [3159] Returning multiple values from a function call in various languages - a comparison - (2011-02-06)
  [3029] PHP data sources - other web servers, large data flows, and the client (browser) - (2010-11-04)
  [2964] An introduction to file handling in programs - buffering, standard in and out, and file handles - (2010-09-21)
  [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
  [3839] Spraying data from one incoming to series of outgoing files in Perl - (2012-08-15)
  [3830] Traversing a directory in Perl - (2012-08-08)
  [3548] Dark mornings, dog update, and Python and Lua courses before Christmas - (2011-12-10)
  [3326] Finding your big files in Perl - design considerations beyond the course environment - (2011-06-14)
  [2833] Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27)
  [2821] Chancellor George Osborne inspires Perl Program - (2010-06-22)
  [2818] File open and read in Perl - modernisation - (2010-06-19)
  [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)
  [1467] stdout v stderr (Tcl, Perl, Shell) - (2007-12-10)
  [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
  [3764] Shell, Awk, Perl of Python? - (2012-06-14)
  [3558] Python or Lua - which should I use / learn? - (2011-12-21)
  [3465] How can I do an FTP transfer in Python? - (2011-10-05)
  [3442] A demonstration of how many Python facilities work together - (2011-09-16)
  [3083] Python - fresh examples from recent courses - (2010-12-11)
  [2870] Old prices - what would the equivalent price have been in 1966? - (2010-07-14)
  [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)


Back to
On cancellations, rebooking, and pricing schemes
Previous and next
or
Horse's mouth home
Forward to
Christmas is coming very early
Some other Articles
An answer to a student asking 'Help'
Looking after you Christmas Customer Crowds
Using English can slow you right down!
Christmas is coming very early
Reading a file multiple times - file pointers
On cancellations, rebooking, and pricing schemes
Useful command or messy screen?
Linux / Unix - layout of operating system files
Copy and paste / cut and paste and other vi techniques
Above the fold with First Great Western
4252 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/1442_Rea ... nters.html • PAGE BUILT: Sun Mar 30 15:20:58 2014 • BUILD SYSTEM: WomanWithCat