3

I have a program that I basically stole from the Qt website to try to get a file to open. The program refuses to open anything I am confused as to why. I have looked for lots of documentation but found nothing can you please explain why it does not work.

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QTextStream>
#include <QString>
MainWindow::MainWindow(QWidget *parent) :
    QWidget(parent)
{
    QFile file("C:/n.txt");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
             return;
    QTextStream in(&file);
    QString f=in.readLine();
    lab =new QLabel("error",this);
    lab->setGeometry(100,100,100,100);
    lab->setText(f);

}
Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
  • 3
    What does the QFile::error() method return ? – Dimitry Ernot Mar 25 '13 at 16:15
  • 1
    How do I use QFile::error(). –  Mar 25 '13 at 16:43
  • Just like any member function. You do have Qt documentation, do you? –  Mar 25 '13 at 17:31
  • Call QFile::error() before you return on the QFile::open failure. – Phlucious Mar 25 '13 at 18:09
  • Could someone give me a code snippet to know how to use it and yes I looked at the qt docs. –  Mar 25 '13 at 18:36
  • 1
    It is _extremely_ easy to use. The easiest way is to store the return value of `QFile::error()` to a variable, then run the code in a debugger and see what that return value is. Then find from the Qt documentation what that error code means. –  Mar 25 '13 at 18:51
  • 1
    And are you really sure that the file `C:\n.txt` exists? Are you sure it is not, for example, `C:\n.txt.txt`? –  Mar 25 '13 at 18:57
  • 1
    thanl you thank you soososossososo much i have been having so many anxiety attacks over this i am so relived turns out it was n.txt.txt i am so dumb thank you –  Mar 26 '13 at 05:18

2 Answers2

7

Before opening the file, you can always check the existense:

QFile file("myfile.txt");
if (!file.exists()) {
    // react
}

If file exists but does not open, you can get the error state and message:

QString errMsg;
QFileDevice::FileError err = QFileDevice::NoError;
if (!file.open(QIODevice::ReadOnly)) {
    errMsg = file.errorString();
    err = file.error();
}

And always: if the file was openend, then remember to close it. In your example you didn't:

file.close();
Ville Lukka
  • 256
  • 1
  • 2
0

FileError QFile::error () const Returns the file error status. The I/O device status returns an error code. For example, if open() returns false, or a read/write operation returns -1, this function can be called to find out the reason why the operation failed. See also unsetError().

Post the error code. Isn't it supposed to be QFile file("C:\n.txt"); \ not / in windows?

arun_vj
  • 123
  • 1
  • 3
  • 13
  • 1
    Then it should be `QFile file("C:\\n.txt");` because backslashes need to be escaped. But / works fine in Windows so there is no need to change it. –  Mar 25 '13 at 17:20
  • Indeed, when supported, using `/` is definitely preferred path separator in strings inside application. No reason to make porting any harder, and no reason to use char which needs escaping inside strings (with the potential to forget, and with bad luck just silently get a wrong char). – hyde Mar 26 '13 at 05:58
  • Don't know if the op found answer to this question, but most of the times, this happens because of permission issues. If you're debugging your program, try opening QtCreator in administrator mode, and then debug. While running your program, open a cmd.exe window in admin mode and run the executable. Also, QFile works both ways: i.e. QFile("C:/n.txt") as well as QFile("C:\\n.txt") on windows, though the preferable way is to use forward slashes in file path. – Manmohan Singh Oct 18 '16 at 10:40