简体   繁体   English

从C ++中的文件读取X行直到Y行

[英]Reading line X until line Y from file in C++

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? 假设我有一个文件,但我只想访问文件的X行直到Y行,那么最简单的方法是什么?

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. 谢谢。

In C++, no, not really (well, not in any language I'm familiar with, really). 在C ++中,不是,不是真的(嗯,不是我所熟悉的任何语言)。

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). 您必须从文件的开头开始,以便可以确定X行的起始位置(除非它是固定记录长度的文件,但是对于文本而言不太可能)。

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. 如果您担心缓冲区溢出漏洞,可以读取字符而不是行,也可以读取固定大小的块并计算换行符的速度,但这归结为读取和检查每个字符(通过代码或语言库)来计算换行符。

You can use istream::ignore() to avoid buffering the unneeded input. 您可以使用istream :: ignore()避免缓冲不需要的输入。

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();
}

Search for \\n X times, start 'reading' (whatever processing that entails) until you reach the YX \\n or EOF. 搜索\\ n X次,开始“读取”(需要进行任何处理),直到达到YX \\ n或EOF。 Assuming unix style new lines. 假设unix样式为新行。

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. 我能想到的唯一的优化方法不是逐行读取文件,而是对其进行缓冲,然后迭代计算行数。

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). 使用C或C ++,存在一些可用于跳过文件中指定字节数的功能(在stdio中为feek,在istream和ostream中为seekp)。 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...) 文件不是某种形式的数组,其中每一行都占据一个“行”:您宁可将其视为连续的存储空间(此处不讨论硬件...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM