Unable to read text file with Qt 5


I wrote a simple code to open plain text file with Qt 5's QFile as seen below;

// main.cpp
#include <iostream>
using std::endl;
using std::cout;

#include <QCoreApplication>
#include <QFile>
#include <QIODevice>

int main(int argc, char *argv[])
    QCoreApplication a(argc, argv);

    QFile plainFile("plain.txt");

    if(plainFile.open(QIODevice::ReadOnly | QIODevice::Text))
        cout << "File opened successfull" << endl;
        cout << "could not open file." << endl;
    return a.exec();

The output when compiled and run is "could not open file". What am I do wrong?

Probably because plain.txt does not exist in the current working directory or in the PATH. Make sure the file is in the working directory or pass the absolute path to QFile.

Also see what QFile::exists returns.