程序应该显示文件的最后5行,但它不适用于大文件

the program should display the last 5 lines of the file, but it does not work with large files

本文关键字:不适用 适用于 文件 5行 显示文件 最后 程序      更新时间:2023-10-16

我写了一个程序,应该打印文件的最后5行,但老师创建了一个行为4GB的文件,程序坏了。如何重写程序,使其能够处理非常大的文件

一个可能的解决方案是逐个字符地读取文件,但我不知道如何做到

这是c++程序代码

#include <iostream>
#include <fstream>
#include <string>
using std::ifstream;
using std::cout;
using std::string;
using std::getline;
int main(int argc, char * argv[], char * env[]) {
setlocale(LC_ALL, "");
int i;
string line;
if (argc == 3) {
string filename = argv[1];
ifstream myfile(filename);
string n = argv[2];
int nn = atoi(n.c_str());
string line, buffer[nn];
const size_t size = sizeof buffer / sizeof * buffer;
size_t i = 0;
while (getline(myfile, line)) {
buffer[i] = line;
if (++i >= size) {
i = 0;
}
}
for (size_t j = 0; j < size; ++j) {
cout << buffer[i] << "n";
if (++i >= size) {
i = 0;
}
}
//return 0;
}
}

问题一定出在4GB文件中的大行上。您的解决方案会缓冲(并在以后丢弃(每一行,其中至少一行可能太长,无法在您正在运行的机器中进行缓冲,从而导致程序崩溃。

您应该从末尾开始读取文件,计算换行数,并在达到nn + 1的计数时停止并输出if的其余部分。当您需要处理大的行时,缓冲最后的nn行不是一个好的选择。

这里有一个可以帮助你的解决方案片段:

array<char, 64 * 1024> buffer; // 64kb of buffer
size_t nn = atoi(n.c_str()); 
myfile.seekg(0, ios_base::end); 
unsigned int nlcount = 0; 
size_t length = myfile.tellg(); 
size_t oldpos = length; 
while (myfile.tellg() > 0) { 
size_t newpos = oldpos - min(oldpos, buffer.size()); 
myfile.seekg(newpos); 
size_t rdsize = oldpos - newpos; 
myfile.read(buffer.data(), rdsize); 
if (!myfile) { 
cerr << "failed while looking for newlinesn"; 
return 1; 
} 
auto rit = buffer.rbegin() + (buffer.size() - rdsize); 
while (rit != buffer.rend() && nlcount <= nn) { 
if (*rit == 'n') { 
++nlcount; 
} 
++rit; 
} 
if (nlcount > nn) { 
myfile.seekg(newpos + (buffer.rend() - rit) + 1); 
break; 
} 
oldpos = newpos; 
} 

如果nlcount等于nn + 1,这将把输入流指向您只需要输出其剩余部分的确切位置。我建议您不要使用缓冲行输出,而是使用固定大小的缓冲区:

while (myfile.peek() != EOF) {
myfile.read(buffer.data(), buffer.size());
cout.write(buffer.data(), myfile.gcount());
}

不要使用getline(),否则在处理长的行时仍然会缓冲行并崩溃。

要删除缓冲区依赖关系,一种方法是从文件末尾向后读取文件,以达到所需的行数。5在这里是硬编码的,但您可以将其作为参数传递。

std::ifstream fileReader("test.txt", std::ios_base::ate );
std::string currentLine;
long length;
int lines;
char c = '';
if( fileReader )
{
length = fileReader.tellg();
for(long i = length-2; i > 0; i-- )
{
fileReader.seekg(i);
c = fileReader.get();
if( c == 'r' || c == 'n' )
{
lines++;
if (lines == 5)
break;
}
}
while(fileReader)
{
std::getline(fileReader, currentLine);
std::cout << currentLine << std::endl;
}
}
相关文章: