Take the 2-minute tour ×  E.g. you have a file with 3 lines that
are millions of characters long. What's the fastest/most
efficient way to read the first N characters of line 2?
share|improve this question Do you know how long line 1 is? – 
Ilya Kogan Apr 5 '11 at 6:53 "Millions of characters" (= dozens
of megabytes) is not a lot by today's standard. When
programming, concentrate on the algorithm and do not perform
optimizations until the program works already and you have
profiled the parts that actually need optimization. –  Giovanni
Funchal Apr 5 '11 at 11:19 add comment  5 Answers  Either you
have some insight into where the line breaks might be...    •
lines are equally long so divide the file size by three and seek
to that address   • only one line's long but you're not sure
which, so read a chunk from the start and end of the file and
work out which line it is  ...or you're completely in the dark
and have to just scan your way along until you find the first
newline:    • use memory mapped I/O for fast