You are not logged in.

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.

1

Wednesday, August 3rd 2005, 9:28pm

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.

jacek

Master

  • "jacek" is male

Posts: 2,729

Location: Warsaw, Poland

  • Send private message

2

Wednesday, August 3rd 2005, 10:29pm

RE: error reading a text file

Quoted

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;
	 }

3

Wednesday, August 3rd 2005, 11:27pm

RE: error reading a text file

Thanks.