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 a binary file

Reading a binary file

Scheduled Pinned Locked Moved General and Desktop
8 Posts 5 Posters 5.0k Views
  • 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.
  • K Offline
    K Offline
    kitten
    wrote on last edited by
    #1

    hi to everyone
    i am trying to read a binary file and save it to my db as a blob data(qsqlite)
    i use this code
    @ QFIle myfile("1.zip");
    QByteArray x;
    if(!myfile.open())
    return;
    while(!myfile.atEnd())
    x = myfile.readall();
    QFIle myfile1("1.png");
    QByteArray x1;
    if(!myfile1.open())
    return;
    while(!myfile1.atEnd())
    x1 = myfile1.readall();
    @
    i think it only read the a few first charachters of my file
    how can i solve this?

    www.kitten.mihanblog.com

    1 Reply Last reply
    0
    • F Offline
      F Offline
      fluca1978
      wrote on last edited by
      #2

      I don't understand what the problem is. How can you say that only a few data is read from the file?

      1 Reply Last reply
      0
      • C Offline
        C Offline
        cincirin
        wrote on last edited by
        #3

        bq. QByteArray can be used to store both raw bytes (including '\0's) and traditional 8-bit '\0'-terminated strings
        Behind the scenes, it always ensures that the data is followed by a '\0' terminator

        Perhaps this is the reason why you see only part of the array.

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

          Additionally, if readall does not return the whole file, you overwrite the previous data.

          @
          while(!myfile.atEnd())
          x += myfile.readall();
          @

          or remove the while loop.

          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
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            readAll is guaranteed to read all available data, so the while loop is completely unnecessary.

            And so you think the data is incomplete? Did you proof yourself it actually is incomplete?

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

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kitten
              wrote on last edited by
              #6

              yes i am sure the data is incomplete
              because the file is abour 200 kb but the x variable has only a few charachter(4 or 5 charachter)

              www.kitten.mihanblog.com

              1 Reply Last reply
              0
              • C Offline
                C Offline
                cincirin
                wrote on last edited by
                #7

                What is the value of @x.size()@ ?

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

                  You're speaking of charactares, which makes me guess that you're on the wrong concept.

                  You must not print arbitrary binary data with printf() or qDebug()! As cincirin already mentioned, this stops at the first null-byte (0x00) regardless how big your data is.

                  You must check the byte array's size with x.size().

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

                  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