in reply to Index a file with pack for fast access

Asking for line 3 using this code gives me back: size is 4 offset is 12 found line 39109 1 (incorrect data that appears to be from the middle of line 5)

There is an error in your logic. The offset for line 3 should be 8. Line 1 is offset 0, line 2 is 4, line 3 is 8. The lines are one-based and the index offsets are zero-based so the offset to the index would be size * (line-1).

A similar one-off error is in your index writing sub. You never write the offset to the last line of the text file. If I were writing this I would copy BrowserUk's example and simply write an offset of 0 to the index file straight away.

  • Comment on Re: Index a file with pack for fast access