You don't necessarily want to go through the whole file, it could be gigabytes. I'd try jumping back a block at a time and counting the number of lines in each block.
When you have found enough lines, just start going forward again. You could probably save the lines that you find in a buffer, instead of reading them from the file again, but that would depend on how much memory you want to use.
As to how big a block is, I'm not sure - I'd probably start with 4k but I couldn't tell you why, I just like 4k
