简体   繁体   中英

Do-while infinite loop in Qt

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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