Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Reading the file content
Qt 6.11 is out! See what's new in the release blog

Reading the file content

Scheduled Pinned Locked Moved General and Desktop
5 Posts 5 Posters 3.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Indrajeet
    wrote on last edited by
    #1

    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]

    1 Reply Last reply
    0
    • L Offline
      L Offline
      LinusA
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          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.

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

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

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved