Important: Please read the Qt Code of Conduct -

QTextStream crashes when reading a file

  • Hello,

    I have the following code:

        QFile fFile(strFile);
        if(! | QIODevice::Text))
            qDebug() << "can't read";
        else {
            qDebug() << "executed";
            QTextStream in(&fFile);
            qDebug() << "not executed";
            while(!in.atEnd()) {
                qDebug() << in.readLine();

    Most of the time when the code is executed it crashes at the line QTextStream in(&fFile).
    The point is that I use the same code somewhere else to read other files and it is working absolutely fine. It is also very strange, that it works in very rare cases. There is definitively no problem with the file itself because there error occurs independent from what "strFile" contains (as long as it is a valid file).
    I really don't know what's going on here. The underlaying OS is Windows 7.

    Any ideas?

  • Could you post the stack trace of the crash? What version of Qt are you using? any chance fFile goes out of scope before entering the QTextStream in(&fFile); line?

  • oooooh well :(

    Sorry guys, false alarm. I found the mistake somewhere else in the code. Wanted to write on a position in an array that wasn't available so far. Don't know how I made the connection to QTextStream.
    I didn't see the forest for the trees.

    Has been a long day, I should go home now. Sorry :)

Log in to reply