Reputation: 2808
QLabel* codeLabel = new Qlabel;
QFile file("C:\index.txt");
file.open(stderr, QIODevice::WriteOnly);
QByteArray data;
data = file.readAll();
codeLabel->setText("test"+QString(data));
file.close();
Then there is only "test" in QLabel. Help, Please
Upvotes: 2
Views: 2152
Reputation: 881093
Aside from the fact you should escape backslashes within C-style strings (c:\\index.txt
), you have a problem with the following sequence:
// vvvvvvvvv
file.open(stderr, QIODevice::WriteOnly);
:
data = file.readAll();
// ^^^^
What exactly did you think was going to happen when you opened the file write-only, then tried to read it? You need to open it for reading such as with QIODevice::ReadOnly
or QIODevice::ReadWrite
.
On top of that, you should check the return code of all functions that fail by giving you a return code. You currently have no idea whether the file.open()
worked or not.
I'm also not convinced that you should be opening stderr
(which is really an ouput "device") for input. You'll almost certainly never get any actual data coming in on that file descriptor, which is probably why your input is empty.
You need to step back and ask what you're trying to acheive. For example, are you trying to capture everything your process sends to standard error? If so, it's not going to work that way.
If you're just trying to read the index.txt
file, you're using the wrong overload. Remove the stderr
parameter altogether:
file.open (QIODevice::ReadOnly);
If it's something else you're trying to do, add that to the question.
Upvotes: 4
Reputation: 7996
QFile file("C:\index.txt");
Here you try to open a file called: C:index.txt
because '\i'
is converted to i
. You want to double you backslash:
QFile file("C:\\index.txt");
Upvotes: 2
Reputation: 48176
file.open(stderr, QIODevice::WriteOnly);
this closes the file again and reopens with the stderr stream in write only mode
you'll want to change that to
file.open(QIODevice::ReadOnly);
Upvotes: 2
Reputation: 409136
Because you read from a file you opened write-only.
Upvotes: 0