how to efficiently find line number k

B

b83503104

I have a big text file with millions of lines, and given any number k
as input, I want to output line k. What is the most efficient way to
do this, other than checking end-of-line k times?
Thanks
 
N

Neil Kurzman

b83503104 said:
I have a big text file with millions of lines, and given any number k
as input, I want to output line k. What is the most efficient way to
do this, other than checking end-of-line k times?
Thanks

No matter how you do it you will have to read every char before it.

BTW this Q is OT
 
R

Richard Bos

I have a big text file with millions of lines, and given any number k
as input, I want to output line k. What is the most efficient way to
do this, other than checking end-of-line k times?

For a one-shot run, that _is_ the most efficient way. If you run this
search repeatedly, and you know that the text file is not going to
change, you can create an index file (or even an index in memory, if you
want to waste several millions of bytes on it). If you run this search
repeatedly, and the text file does change, probably the most efficient
solution is to get the program that changes it to also update the index.
Basically, you'd be turning your text file into a database of sorts.

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top