Random access to text file
I have a text file where each line correspond to a specific entry. Unfortunately, lines are of variable size.
At some point, I need to access entry X, which I know to be at line X in my text file. Unfortunately, I have no clue what is the byte position of entry X. Of course, I want to avoid reading all of the X-1 first lines.
I am wondering if there is any way to access a text file randomly based on line number instead of byte position ?
If not, suggestions are welcome.
An obvious solution would be to keep an index file with entries of the form:
"Entry X : byte position of entry X in data file"
So each line of the index file has a constant size.However, this is a bit tedious as modification to the main file implies modification of the whole index file.... other solutions ?