PDA

View Full Version : QFile can't read a file



Raccoon29
11th February 2009, 11:27
Hi everyone,

I have a file .txt that, got from Internet, can be read by every text editor, from SCiTe to Notepad, and from WordPad to Word; but QFile can't read it.

Used procedure to open and read the file is very tested, so cannot be its fault.
If I cut/paste the content in a new file, than QFile read it normally.

It may seem a corrupted file, but if it is, then why text editors can open it?
Trying to read it by QFile, readLine() returns just 3 meaningless chars and testing around I got this informations (right after opened):

file.openMode => ReadOnly | Text
file.isOpen() => true
file.canReadLine() => false
file.readLine() => XXX (3 random chars)
file.size() => 982 (actual file size: 982 bytes)
file.atEnd() => false (after readLine())

that's it.
Is someone able to try an explanation? :confused:

jpn
11th February 2009, 14:08
I have a file .txt that, got from Internet
Downloaded programmatically?


can be read by every text editor, from SCiTe to Notepad, and from WordPad to Word
Presumably after your application has closed?


but QFile can't read it.
Perhaps the file is still open for writing?

Raccoon29
11th February 2009, 18:55
Downloaded programmatically?
No, downloaded manually, I forgot to explain it...


Presumably after your application has closed?
Well, no matter.


Perhaps the file is still open for writing?
No, because I open the file just for reading.

It seems that I could have found the problem: if opened in a hexadecimal editor, the txt presents a pair of meaningless chars at the beginning, such as FF and FE, than it has a 00 between every char in the file.

Bad story. Because of this QFile can't read it properly! But normal editors can instead...
How is it possible?

caduel
11th February 2009, 20:24
Seems like the file is in UTF-16 encoding or something like that.
So it is not "plain text". You need to set the proper encoding on the stream and perhaps not open it as "text".