|
Re: Jumping to a specific line number in a text file
A file with variable length lines has no way to directly address lines other than by counting line endings. Line endings are not magical, they're just another binary code, like the rest of the file.
A file with fixed length lines, via padding, can be address by using fseek (or the appropriate equivalent).
Disk I/O is orders of magniture slower than memory access, so your final question depends upon whether you will be accessing the data more than once, or in random order. In the latter case, reading lines into a 2D array would be advisable sometimes, depending upon the probability that you might have to access any line in the file.
You're going to have to read all the lines that occur before the latest line you have to deal with (assuming the variable length lines). If there's a significant probability that you're going to need the earlier lines at some point, then you may as well save them in memory and preclude having to do the slow mechanical operations more than once.
You might also investigate memory-mapped files.
It's situations like this that take you beyond your language and into the implementation of the system as a piece of hardware and as a system governed by some particular OS and the functions thereof.
If you're going to encounter this sort of thing very often, digging a little deeper into the actual implementations might be advisable.
|