Reading the file content



  • Hi All

    I have a file of size 1GB,I was reading this file using below code

    QString data = file.readAll();

    But the above code is not working properly.

    Can any one tell me which is the best way to read the contents of big file but i want data to be in QString[i.e full file content]



  • [quote author="Rajveer" date="1314157903"]
    But the above code is not working properly.
    [/quote]
    What does that mean? Post an error message! If it doesn't crash: What results do you expect for data, what do you observe?



  • Do you really, really want to read a 1GB file in one go, and put it all in a QString? Note that QString internally uses UTF-16 as the storage format; that means at least two bytes per character. Depending on the encoding of the file you are trying to read, that might result in a 2 GB block of memory needed for your QString alone.

    Think if you can read and process the file in pieces instead.



  • Oh, and you're probably doubling the memory consumption, as readAll() returns a QByteArray, that is fed to the QString constructor, which most probably will copy the data. So you will have 1GB in the QByteArray and roughly 2GB in the QString. In total you need 3GB - at least until the QString constructor is done.



  • Even if you don't read it into one qstring, a file of size 1GB totally copied into RAM is really huge. Could it be read and worked in chunks? That would make the whole stuff much faster....


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.