Dear visitor, welcome to QtForum.org.
If this is your first visit here, please read the Help. It explains in detail how this page works.
To use all features of this page, you should consider registering.
Please use the registration form, to register here or read more information about the registration process.
If you are already registered, please login here.
error reading a text file
I am trying to read a text file. This is my code:
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
QString contents;
QFile file("data.txt");
if(!file.open(IO_WriteOnly | IO_Translate))
{
QMessageBox::information(this, "Error", "Could not open file for writing");
return;
}
QTextStream in(&file);
in.setEncoding(QTextStream::UnicodeUTF8);
contents=in.read();
if(file.status() !=IO_Ok)
{
QMessageBox::information(this, "Error", "Could not write to file");
return;
}
|
However, all I get is an empty string. Please help.
RE: error reading a text file
Originally posted by msosno01
I am trying to read a text file.
Are you sure, you wanted to
read that file? Never cut & paste even the smallest part of the source code, rewrite it instead --- it takes less time

|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
QString contents;
QFile file("data.txt");
if(!file.open(IO_WriteOnly | IO_Translate))
^^^^^^^^^^^^
{
QMessageBox::information(this, "Error", "Could not open file for writing");
^^^^^^^^^^^^
return;
}
QTextStream in(&file);
in.setEncoding(QTextStream::UnicodeUTF8);
contents=in.read();
if(file.status() !=IO_Ok)
{
QMessageBox::information(this, "Error", "Could not write to file");
^^^^^^^^^
return;
}
|
RE: error reading a text file