Results 1 to 7 of 7

Thread: Can't read large file (2.5GB) with QFile

  1. #1
    Join Date
    Dec 2010
    Posts
    55
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Can't read large file (2.5GB) with QFile

    I am trying to read a 2.5GB text file.

    Qt Code:
    1. QFile input_file(filename);
    2. if (!input_file.open(QIODevice::ReadOnly))
    3. return;
    4. qint64 x = input_file.bytesAvailable();
    5. QString test = input_file.readLine();
    6. QString test2 = input_file.readAll();
    To copy to clipboard, switch view to plain text mode 

    Both test and test2 are empty.
    x = 2682500677
    Also, isReadble() is true, but canReadLine() is false
    I can read a 5MB file with no problem.
    Running:
    RHEL 5.4
    Python 2.7.2
    Qt 4.7.4
    SIP 4.7.8
    PyQt 4.7

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Can't read large file (2.5GB) with QFile

    QIODevice::readLine() and QIODevice::readAll() return QByteArrays not QStrings. The QString is being constructed using the conversion constructor, which uses QString::fromAscii(). Depending on the current codec if the first byte of the file is not valid, e.g. a UTF-8 byte-order-mark, then you may well end up with an empty string.

    So, what does you file actually contain, and is it valid characters only?


    ...
    On second thoughts, even if your byte array slurped the entire file into memory, conversion to 16-bit characters in a QString will double the size and blow the 4GB limit on 32-bit architectures.
    Last edited by ChrisW67; 15th July 2011 at 01:49.

  3. #3
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Can't read large file (2.5GB) with QFile

    Yeah, I'm guessing he's out of storage, one way or another.

  4. #4
    Join Date
    Dec 2010
    Posts
    55
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Can't read large file (2.5GB) with QFile

    I am on a 64-bit RHEL5 system

    The file is all text.

    Upon further examination, readLine() does work. the first line in the file is blank, if I read the second line, I could see the text. Although this seems to get stuck once the program hits 1.0gb of resident memory usage.

    readAll() just seems to give up and not even try. I looked into it more closely and it looks like it fails to resize the QByteArray to 2.5gb. I would have expected an error but I guess that makes sense.
    I've got 12GB ram on this system.
    Last edited by enricong; 15th July 2011 at 14:27.
    Running:
    RHEL 5.4
    Python 2.7.2
    Qt 4.7.4
    SIP 4.7.8
    PyQt 4.7

  5. #5
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Can't read large file (2.5GB) with QFile

    What is the output of 'ulimit -a'? And what context does your code occur in - main() or within a function?

  6. #6
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Can't read large file (2.5GB) with QFile

    The amount of RAM you have on the system has little to do with this. A 4-byte number can represent a bit over 4g, and many environments limit things to half that so that arithmetic will be signed. In Qt the array size and object length fields are 4 bytes long.

    It's very poor programming practice to read an entire file larger than a megabyte or so anyway. It causes storage thrashing and really mucks up performance.


    Added after 9 minutes:


    Of course, the other option would be to convert the file to a SQL database.
    Last edited by DanH; 15th July 2011 at 21:10.

  7. #7
    Join Date
    Nov 2010
    Posts
    13
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Can't read large file (2.5GB) with QFile

    Is your mem enough? Maybe the way is on another.

Similar Threads

  1. Read and Write file to/from QFile
    By ruben.rodrigues in forum Qt Programming
    Replies: 1
    Last Post: 31st March 2011, 14:57
  2. QFile - file read parts of the buffer
    By eugen_Qt in forum Qt Programming
    Replies: 4
    Last Post: 10th November 2009, 14:52
  3. Replies: 5
    Last Post: 27th May 2009, 12:49
  4. QFile can't read a file
    By Raccoon29 in forum Qt Programming
    Replies: 3
    Last Post: 11th February 2009, 20:24

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.