Why does QTextStream::pos() return -1 while reading a file line-by-line in Qt?
-
I'm reading an NMEA file line by line using QTextStream, and I noticed that at some point, QTextStream::pos() suddenly returns -1 even though I haven’t reached the end of the file yet.
Here’s the relevant part of my function:
void MainWindow::OnNmeaReadFileTimer() { if(!ui->play_button->isChecked()) { return; } qDebug() << "starting from pos:" << m_readNmeaTextStream.pos() << "and lineNum:" << lineNum; QString payload; QString nmea_payload; QString nmea_packet; QString nmea_packet_with_time; uint32_t previousLinePos = 0; int cnt = 0; while (!m_readNmeaTextStream.atEnd()) { qDebug() << "reading pos:" << m_readNmeaTextStream.pos() << "and linenumber:" << lineNum; previousLinePos = m_readNmeaTextStream.pos(); payload = m_readNmeaTextStream.readLine(); qDebug() << "data read:" << payload; nmea_payload = payload; lineNum++; if (payload.startsWith('$')) payload.prepend("\n" + QString("%1").arg(lineNum, 8, 10, QChar('0')) + "\t"); else payload.prepend(QString("%1").arg(lineNum, 8, 10, QChar('0')) + "\t"); if (nmea_payload.contains("RMC")) { if (++cnt >= 2) { cnt = 0; qDebug() << "previouspos:" << previousLinePos; m_readNmeaTextStream.seek(previousLinePos); lineNum--; break; } } nmea_packet.append(nmea_payload); nmea_packet_with_time.append(payload); qDebug() << "actual line num:" << lineNum; qDebug() << "attached linenum:" << nmea_packet_with_time; qDebug() << "stream pos:" << m_readNmeaTextStream.pos(); } if (m_readNmeaTextStream.atEnd()) { if (m_pReadNmeaFilePollTimer && m_pReadNmeaFilePollTimer->isActive()) { m_pReadNmeaFilePollTimer->stop(); } } ui->file_slider->setValue(lineNum); ui->line_no_spinBox->setValue(lineNum); if (nmea_packet.contains("TXT")) { QByteArray nmeaBytes = nmea_packet.toUtf8(); utils->ProcessNMEABuffer(nmeaBytes.data(), nmeaBytes.size()); } emit update_nmea_log_payload(nmea_packet_with_time); qApp->processEvents(); } void MainWindow::find_rmc(int value) { m_pReadNmeaFile->seek(0); m_readNmeaTextStream.resetStatus(); // Clear EOF/error flags m_readNmeaTextStream.seek(0); int i; for (i = 0; i < value && !m_readNmeaTextStream.atEnd(); i++) { QString skipping = m_readNmeaTextStream.readLine(); } qint64 nextRmcPos = -1; qint64 nextRmcLineNo = value; while (!m_readNmeaTextStream.atEnd()) { nextRmcPos = m_readNmeaTextStream.pos(); QString nextRmcLine = m_readNmeaTextStream.readLine(); if (nextRmcLine.contains("RMC", Qt::CaseInsensitive)) { break; } nextRmcLineNo++; } if (nextRmcPos != -1) { // Seek to the START of the RMC line, not +1 m_readNmeaTextStream.seek(nextRmcPos); lineNum = nextRmcLineNo; // Set to the RMC line number } else { // Position at the target line since no RMC was found m_pReadNmeaFile->seek(0); m_readNmeaTextStream.resetStatus(); m_readNmeaTextStream.seek(0); for (int j = 0; j < value && !m_readNmeaTextStream.atEnd(); j++) { m_readNmeaTextStream.readLine(); } lineNum = value; } }At runtime, I see the following output:
starting from pos: 25060 and lineNum: 540 reading pos: 25060 and linenumber: 540 data read: "GNRMC,024931.00,V,,,,,0.00,,060180,,,N*7F" actual line num: 541 attached linenum: "00000541\tGNRMC,024931.00,V,,,,,0.00,,060180,,,N*7F" stream pos: -1 reading pos: -1 and linenumber: 541As you can see, after getting the next rmc position then after the next one read, m_readNmeaTextStream.pos() becomes -1.
I expected it to continue increasing normally as I read the file. -
I'm reading an NMEA file line by line using QTextStream, and I noticed that at some point, QTextStream::pos() suddenly returns -1 even though I haven’t reached the end of the file yet.
Here’s the relevant part of my function:
void MainWindow::OnNmeaReadFileTimer() { if(!ui->play_button->isChecked()) { return; } qDebug() << "starting from pos:" << m_readNmeaTextStream.pos() << "and lineNum:" << lineNum; QString payload; QString nmea_payload; QString nmea_packet; QString nmea_packet_with_time; uint32_t previousLinePos = 0; int cnt = 0; while (!m_readNmeaTextStream.atEnd()) { qDebug() << "reading pos:" << m_readNmeaTextStream.pos() << "and linenumber:" << lineNum; previousLinePos = m_readNmeaTextStream.pos(); payload = m_readNmeaTextStream.readLine(); qDebug() << "data read:" << payload; nmea_payload = payload; lineNum++; if (payload.startsWith('$')) payload.prepend("\n" + QString("%1").arg(lineNum, 8, 10, QChar('0')) + "\t"); else payload.prepend(QString("%1").arg(lineNum, 8, 10, QChar('0')) + "\t"); if (nmea_payload.contains("RMC")) { if (++cnt >= 2) { cnt = 0; qDebug() << "previouspos:" << previousLinePos; m_readNmeaTextStream.seek(previousLinePos); lineNum--; break; } } nmea_packet.append(nmea_payload); nmea_packet_with_time.append(payload); qDebug() << "actual line num:" << lineNum; qDebug() << "attached linenum:" << nmea_packet_with_time; qDebug() << "stream pos:" << m_readNmeaTextStream.pos(); } if (m_readNmeaTextStream.atEnd()) { if (m_pReadNmeaFilePollTimer && m_pReadNmeaFilePollTimer->isActive()) { m_pReadNmeaFilePollTimer->stop(); } } ui->file_slider->setValue(lineNum); ui->line_no_spinBox->setValue(lineNum); if (nmea_packet.contains("TXT")) { QByteArray nmeaBytes = nmea_packet.toUtf8(); utils->ProcessNMEABuffer(nmeaBytes.data(), nmeaBytes.size()); } emit update_nmea_log_payload(nmea_packet_with_time); qApp->processEvents(); } void MainWindow::find_rmc(int value) { m_pReadNmeaFile->seek(0); m_readNmeaTextStream.resetStatus(); // Clear EOF/error flags m_readNmeaTextStream.seek(0); int i; for (i = 0; i < value && !m_readNmeaTextStream.atEnd(); i++) { QString skipping = m_readNmeaTextStream.readLine(); } qint64 nextRmcPos = -1; qint64 nextRmcLineNo = value; while (!m_readNmeaTextStream.atEnd()) { nextRmcPos = m_readNmeaTextStream.pos(); QString nextRmcLine = m_readNmeaTextStream.readLine(); if (nextRmcLine.contains("RMC", Qt::CaseInsensitive)) { break; } nextRmcLineNo++; } if (nextRmcPos != -1) { // Seek to the START of the RMC line, not +1 m_readNmeaTextStream.seek(nextRmcPos); lineNum = nextRmcLineNo; // Set to the RMC line number } else { // Position at the target line since no RMC was found m_pReadNmeaFile->seek(0); m_readNmeaTextStream.resetStatus(); m_readNmeaTextStream.seek(0); for (int j = 0; j < value && !m_readNmeaTextStream.atEnd(); j++) { m_readNmeaTextStream.readLine(); } lineNum = value; } }At runtime, I see the following output:
starting from pos: 25060 and lineNum: 540 reading pos: 25060 and linenumber: 540 data read: "GNRMC,024931.00,V,,,,,0.00,,060180,,,N*7F" actual line num: 541 attached linenum: "00000541\tGNRMC,024931.00,V,,,,,0.00,,060180,,,N*7F" stream pos: -1 reading pos: -1 and linenumber: 541As you can see, after getting the next rmc position then after the next one read, m_readNmeaTextStream.pos() becomes -1.
I expected it to continue increasing normally as I read the file.@stash22 said in Why does QTextStream::pos() return -1 while reading a file line-by-line in Qt?:
m_readNmeaTextStream.pos() becomes -1.
The documentation states this: https://doc.qt.io/qt-6/qtextstream.html#pos