0

I'm trying to read in a file of trace addresses (each on their own line) and append to the front of each. This input file is intended to be the engine of a cache emulator i'm trying to build. I am having issues reading the file in without getting into an infinite loop. When I change the do-while to run on a false condition, I get the proper output for just the do segment. Therefore, I know I'm running into an infinite loop issue with how I worded my while segment. Maybe i'm fatigued and can't see the issue with this function:

void MainWindow::readFile(){
    infoLabel->setText(tr("Invoked <b>File|Open</b>"));
    QString filename="trace.txt";
    QString path = QDir::currentPath();
    QFile file("//Users//nathan1324//Desktop//trace.txt");
    //file.open(QIODevice::ReadOnly);
    if(!file.exists()){
        qDebug() << "File cannot be found "<<filename;
        qDebug() << " " << path;
    }else{
        qDebug() << filename<<" Opening...";
    }
    QString line;
    textEdit->clear();
    if (file.open(QIODevice::ReadOnly | QIODevice::Text)){
        QTextStream stream(&file);
        do {
            line = stream.readLine();
            textEdit->setText(textEdit->toPlainText()+"0x"+line+"\n");
            qDebug() << "line: "<<line;
        } while (!line.isNull());
    }
    file.close();
}

Any suggestions of an alternative way to write this function?

Nathan1324
  • 158
  • 1
  • 2
  • 12
  • Have you tried stepping through the loop in a debugger? To see what data you read and that `line.isNull()` actually is true at the end of the file? Have you tried [`isEmpty`](http://doc.qt.io/qt-5/qstring.html#isEmpty) instead? Or checking the streams [`atEnd`](http://doc.qt.io/qt-5/qtextstream.html#atEnd)? (And checking should really be done *before* you try to add the (possibly null or empty) text to the text box) – Some programmer dude Jun 28 '17 at 13:41
  • I tried changing the loop to do.....while(line.isNull()) , and that gave me only just the first line successfully as output and in the debugger. The debugger is what is in the infinite loop. The actual output, as written above, isn't even making it to the interface. Just tried while(!line.isEmpty()) and I also got stuck in an an endless loop. – Nathan1324 Jun 28 '17 at 13:45
  • Works for me... is the file constantly updated? If so, then an infinite loop would be expected – cbuchart Jun 28 '17 at 13:55

2 Answers2

0

Use atEnd to detect the end of a stream:

bool QTextStream::atEnd() const

Returns true if there is no more data to be read from the QTextStream; otherwise returns false. This is similar to, but not the same as calling QIODevice::atEnd(), as QTextStream also takes into account its internal Unicode buffer.

   while (!stream.atEnd()) {
        line = stream.readLine();
        textEdit->setText(textEdit->toPlainText()+"0x"+line+"\n");
        qDebug() << "line: "<<line;
    }
AnatolyS
  • 4,249
  • 18
  • 28
  • 2
    That's basically the same as `while (!file.eof())` using a standard C++ stream, [and that is wrong](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – Some programmer dude Jun 28 '17 at 14:02
0

To add items use the append function of QTextEdit:

void QTextEdit::append(const QString & text)

Appends a new paragraph with text to the end of the text edit.

Note: The new paragraph appended will have the same character format and block format as the current paragraph, determined by the position of the cursor.

To iterate through the QTextStream atEnd()

bool QTextStream::atEnd() const

Returns true if there is no more data to be read from the QTextStream; otherwise returns false. This is similar to, but not the same as calling QIODevice::atEnd(), as QTextStream also takes into account its internal Unicode buffer.

Code:

void MainWindow::readFile(){
    infoLabel->setText(tr("Invoked <b>File|Open</b>"));
    QString filename = "trace.txt";
    QString path = QDir::currentPath();
    QFile file("//Users//nathan1324//Desktop//trace.txt");
    if(!file.exists()){
        qDebug() << "File cannot be found "<<filename;
        qDebug() << " " << path;
        return;
    }

    QString line;
    textEdit->clear();
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
        qDebug() << "Could not open file" << filename;
        return;
    }

    qDebug() << filename<<" Opening...";

    QTextStream stream(&file);

    while (!stream.atEnd()) {
        line = stream.readLine();
        if(!line.isNull()){
            textEdit->append("0x"+line);
            qDebug() << "line: "<<line;
        }
    }
    file.close();
}
Azeem
  • 11,148
  • 4
  • 27
  • 40
eyllanesc
  • 235,170
  • 19
  • 170
  • 241