8.12. Using Random-Access I/OProblemYou have to read a binary record from the middle of a large file but don't want to read a record at a time to get there. SolutionOnce you know the record's size, multiply it by the record number to get the byte address, and then seek to that byte address and read the record: $ADDRESS = $RECSIZE * $RECNO; seek(FH, $ADDRESS, 0) or die "seek:$!"; read(FH, $BUFFER, $RECSIZE); Discussion
The Solution assumes the first record has a $ADDRESS = $RECSIZE * ($RECNO-1); This won't work on a text file unless all lines are the same length. This is rarely the case. See Also
The Copyright © 2002 O'Reilly & Associates. All rights reserved. |
|