Below is a scheme I use to load a large file. But when the file exceeds 5,242,880 bytes, an extra new line is inserted. So character 5.242.881 is inserted on the next line.
I’ve created a text file with 32767 lines of exactly 160 bytes (including 2 bytes for \r\n, thus 158 characters + \r\n).
Line 32768 has exactly 80 characters. This file load fine.
When I add 1 character to line 32768, this character will be displayed on a new line. Obviously not correct.
Is this a bug in my code or somewhere else?
void MainWindow::openFile(QString filePath)
{
ui->plainTextEdit->clear();
file->setFileName(filePath);
if (file->open(QFile::ReadOnly))
QTimer::singleShot(10, this, SLOT(readFilePart()));
startRead.start();
ui->plainTextEdit->viewport()->setCursor(Qt::WaitCursor);
ui->plainTextEdit->setUpdatesEnabled(false);
}
void MainWindow::readFilePart()
{
qint64 bytesRead = file->read(buffer, bufsize - 1);
buffer[bytesRead] = 0;
ui->plainTextEdit->appendPlainText(buffer);
qDebug() << "duration:" << startRead.elapsed() << ", bytesRead:" << bytesRead;
if (bytesRead == bufsize - 1)
QTimer::singleShot(10, this, SLOT(readFilePart()));
else
{
qDebug() << "rest: {" << buffer << "}";
QTimer::singleShot(10, this, SLOT(closeFile()));
}
}
void MainWindow::closeFile()
{
file->close();
ui->plainTextEdit->setUpdatesEnabled(true);
ui->plainTextEdit->viewport()->unsetCursor();
}
project files are at:
MainWindow.ui [pastebin.com]
MainWindow.h [pastebin.com]
MainWindow.cpp [pastebin.com]
↧