Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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
Y110 - Python - File Handling
  [114] Relative or absolute milkman - (2004-11-10)
  [183] The elegance of Python - (2005-01-19)
  [2011] Conversion of OSI grid references to Eastings and Northings - (2009-01-28)
  [2282] Checking robots.txt from Python - (2009-07-12)
  [2870] Old prices - what would the equivalent price have been in 1966? - (2010-07-14)
  [3083] Python - fresh examples from recent courses - (2010-12-11)
  [3442] A demonstration of how many Python facilities work together - (2011-09-16)
  [3465] How can I do an FTP transfer in Python? - (2011-10-05)
  [3558] Python or Lua - which should I use / learn? - (2011-12-21)
  [3764] Shell, Awk, Perl of Python? - (2012-06-14)
  [4438] Loving programming in Python - and ready to teach YOU how - (2015-02-22)
  [4451] Running an operating system command from your Python program - the new way with the subprocess module - (2015-03-06)
  [4593] Command line parameter handling in Python via the argparse module - (2015-12-08)
  [4663] Easy data to object mapping (csv and Python) - (2016-03-24)
  [4708] Scons - a build system in Python - building hello world - (2016-10-29)
  [4717] with in Python - examples of use, and of defining your own context - (2016-11-02)

P207 - Perl - File Handling
  [12] How many people in a room? - (2004-08-12)
  [255] STDIN, STDOUT, STDERR and DATA - Perl file handles - (2005-03-23)
  [616] printf - a flawed but useful function - (2006-02-22)
  [618] Perl - its up to YOU to check your file opened - (2006-02-23)
  [702] Iterators - expressions tha change each time you call them - (2006-04-27)
  [867] Being sure to be positive in Perl - (2006-09-15)
  [1312] Some one line Perl tips and techniques - (2007-08-21)
  [1416] Good, steady, simple example - Perl file handling - (2007-10-30)
  [1467] stdout v stderr (Tcl, Perl, Shell) - (2007-12-10)
  [1709] There is more that one way - Perl - (2008-07-14)
  [1841] Formatting with a leading + / Lua and Perl - (2008-10-15)
  [1860] Seven new intermediate Perl examples - (2008-10-30)
  [1861] Reactive (dynamic) formatting in Perl - (2008-10-31)
  [2233] Transforming data in Perl using lists of lists and hashes of hashes - (2009-06-12)
  [2405] But I am reading from a file - no need to prompt (Perl) - (2009-09-14)
  [2818] File open and read in Perl - modernisation - (2010-06-19)
  [2821] Chancellor George Osborne inspires Perl Program - (2010-06-22)
  [2833] Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27)
  [3326] Finding your big files in Perl - design considerations beyond the course environment - (2011-06-14)
  [3548] Dark mornings, dog update, and Python and Lua courses before Christmas - (2011-12-10)
  [3830] Traversing a directory in Perl - (2012-08-08)
  [3839] Spraying data from one incoming to series of outgoing files in Perl - (2012-08-15)

H109 - PHP - Input / Output
  [653] Easy feed! - (2006-03-21)
  [709] Handling huge data files in PHP - (2006-05-04)
  [997] Most recent file in a directory - PHP - (2006-12-18)
  [1094] PHP fread - truncated data - (2007-02-27)
  [1096] Sample script - FTP to get a file from within PHP - (2007-03-01)
  [1113] File and URL reading in PHP - (2007-03-20)
  [1780] Server overloading - turns out to be feof in PHP - (2008-09-01)
  [2964] An introduction to file handling in programs - buffering, standard in and out, and file handles - (2010-09-21)
  [3029] PHP data sources - other web servers, large data flows, and the client (browser) - (2010-11-04)
  [3159] Returning multiple values from a function call in various languages - a comparison - (2011-02-06)
  [3424] Divide 10000 by 17. Do you get 588.235294117647, 588.24 or 588? - Ruby and PHP - (2011-09-08)
  [4483] Moving from mysql to mysqli - simple worked example - (2015-05-03)


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
4759 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, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 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., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/1442_.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb