Results 1 to 4 of 4

Thread: QFile can't read a file

  1. #1
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default QFile can't read a file

    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?
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QFile can't read a file

    Quote Originally Posted by Raccoon29 View Post
    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?
    J-P Nurmi

  3. #3
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QFile can't read a file

    Quote Originally Posted by jpn View Post
    Downloaded programmatically?
    No, downloaded manually, I forgot to explain it...

    Quote Originally Posted by jpn View Post
    Presumably after your application has closed?
    Well, no matter.

    Quote Originally Posted by jpn View Post
    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?
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QFile can't read a file

    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".

Similar Threads

  1. [Java] read and write a file
    By mickey in forum General Programming
    Replies: 3
    Last Post: 22nd June 2008, 11:43
  2. Replies: 1
    Last Post: 20th June 2008, 19:43
  3. Read binary file and convert to QString
    By jaca in forum Qt Programming
    Replies: 12
    Last Post: 14th June 2008, 00:05
  4. How to read text only as it is from file
    By thomasjoy in forum Qt Programming
    Replies: 3
    Last Post: 9th August 2007, 09:47
  5. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 16:21

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.