Search code examples
c++qtqfile

QFile: directory not found


I need to write a console application that takes a file, it opens it, and then it calls another procedure based on the information inside the text file. The only problem is that QFile::errorString() returns:

No such file or directory.

I have been using this implementation in all the programs I had to, and yes, the file exists at that directory.

The code is:

QFile fileName("D:/file.txt");
QString read_from_file;
if(fileName.open(QIODevice::ReadOnly)){
    QTextStream in(&fileName);
    while(!in.atEnd())
    {
        read_from_file = in.readLine();
        qDebug()<<read_from_file;
    }
    fileName.close();
 }
qDebug()<<fileName.errorString();

Solution

  • Make sure that the file really exists.


    As you pointed out in the comments, the problem was the hidden file extensions on Windows.

    1. Open Folder Options by clicking the Start button, clicking Control Panel, clicking Appearance and Personalization, and then clicking Folder Options.

    2. Click the View tab, and then Advanced settings <...>

      • To show file name extensions, clear the Hide extensions for known file types check box, and then click OK.