tags:

views:

573

answers:

5

I have a relatively simple question. Say I have a file but I only want to access line X of the file until line Y, whats the easiest way of doing that?

I know I can read in the lines one by one keeping count, until I reach the lines that I actually need, but is there a better more elegant solution?

Thanks.

+5  A: 

In C++, no, not really (well, not in any language I'm familiar with, really).

You have to start at the start of the file so you can figure where line X starts (unless it's a fixed-record-length file but that's unlikely for text).

Similarly, you have to do that until you find the last line you're interested in.

You can read characters instead of lines if you're scared of buffer overflow exploits, or you can read in fixed-size block and count the newlines for speed but it all boils down to reading and checking every character (by your code explicitly or the language libraries implicitly) to count the newlines.

paxdiablo
+2  A: 

Search for \n X times, start 'reading' (whatever processing that entails) until you reach the Y-X \n or EOF. Assuming unix style new lines.

apphacker
+2  A: 

Since you have to ensure end line characters inside each line in order to be able to count line, you do still need to iterate over you file. The only optimization I can think about is not read the file line by line, but buffer it and then iterate counting the lines.

Artem Barger
+4  A: 

You can use istream::ignore() to avoid buffering the unneeded input.

bool skip_lines(std::istream &is, std::streamsize n)
{
  while(is.good() && n--) {
     is.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  }

  return is.good();
}
Dingo
Good solution but I would probably return a reference to the stream instead of only its status. That'd be slightly more flexible.
Konrad Rudolph
A: 

Using C or C++, there exist some functions that you can use to skip a specified number of byte within a file (fseek in the stdio and seekp with istreams and ostreams). However, you cannot specify a given number of lines since each line might have a variable number of characters and therefore it's impossible to calculate the correct offset. A file is not some kind of array where each line occupies a "row": you rather have to see it as a continuous memory space (not talking hardware here thought...)

pierrelucbacon