Miguel Portugal
Miguel Portugal

Reputation: 31

C++ Qt - opening text file operation failure

I'm using Qt to develop my C++ application using QML as well.

Here's my code

QFile inputFile("data.txt");
//QFile inputFile("/:data.txt");
qDebug() << "Hello:";
if (!inputFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
    qDebug() << "Wasn't ready:";
}
else{
    qDebug() << "Txt file ready:";
    QTextStream in(&inputFile);
    while ( !in.atEnd() )
    {
        QString line = in.readLine();
        qDebug() << "message: " << line;
    }
}

I was wondering why it doesn't work. The console always prints "Wasn't ready". Please help.

Upvotes: 3

Views: 560

Answers (1)

Anders Abel
Anders Abel

Reputation: 69280

In the error handling block where you do qDebug() << "Wasn't ready:"; you should call inputFile.error() and print out the returned value to get more details of what went wrong.

It might also be an idea to start the program with printing out the current directory, to make sure that the file is searched for in the correct location.

Upvotes: 1

Related Questions