Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Reading a binary file

    General and Desktop
    5
    8
    4477
    Loading More Posts
    • 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
      kitten last edited by

      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 Reply Quote 0
      • F
        fluca1978 last edited by

        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 Reply Quote 0
        • C
          cincirin last edited by

          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 Reply Quote 0
          • G
            giesbert last edited by

            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 Reply Quote 0
            • G
              goetz last edited by

              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 Reply Quote 0
              • K
                kitten last edited by

                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 Reply Quote 0
                • C
                  cincirin last edited by

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

                  1 Reply Last reply Reply Quote 0
                  • G
                    goetz last edited by

                    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 Reply Quote 0
                    • First post
                      Last post