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. write to Qfile adds additional byte

write to Qfile adds additional byte

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 2.4k 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.
  • M Offline
    M Offline
    mobo
    wrote on 22 Nov 2016, 12:50 last edited by
    #1

    I am programming a tool in Qt in which I want to write binary data into a file. Everything works fine except when I am trying to write the decimal value '10' (0000 1010 in binary) into the file. in this case I get an additional byte with the value '0000 1101' in front of the other byte. It doesn't matter how much data I write in the file, as soon as I write a 10 I get another byte.

    I broke it down to the following code:

    #include <QCoreApplication>
    #include <QFile>
    #include <iostream> 
    #include <QString>
    #include <QDataStream>
    
    int main(int argc, char *argv[])
    {
     QCoreApplication a(argc, argv);
    
     QString SavePath;
      SavePath = qApp->applicationDirPath();
      SavePath.append("/test.bin");
    
      QFile file(SavePath.toLocal8Bit().constData());
      if (file.exists())
          file.remove();
      if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
      {
        return -1;
      }
    
      QDataStream out(&file);
      out.setVersion(QDataStream::Qt_4_3);
      out.setByteOrder(QDataStream::LittleEndian);
      out << qint32(10);
    
      int test=10;
      out.writeRawData((char*)&test,4 );
    
      file.write((char*)&test);
    
      return 1;
     }
    

    The output in my file is:

     0d 0a 00 00 00 0d 0a 00 00 00 0d 0a
    

    the three 0x0d bytes are unwanted. I don't get them while writing a '9' or an '11' to the file. I am running out of ideas. Can anyone tell me what I am doing wrong?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 22 Nov 2016, 13:04 last edited by
      #2

      Hi
      QDataStream handles byte order for you
      so i think it is adding info to allow this.
      You use other write methods, ( i think)
      http://doc.qt.io/qt-5/qdatastream.html#raw

      1 Reply Last reply
      1
      • M Offline
        M Offline
        m.sue
        wrote on 22 Nov 2016, 13:09 last edited by
        #3

        Hi,
        you should open a binary file without QIODevice::Text.
        -Michael.

        1 Reply Last reply
        4
        • M Offline
          M Offline
          mobo
          wrote on 22 Nov 2016, 13:12 last edited by
          #4

          Hi mrjj,
          thanks for your answer!
          Yes I read about the possibility that QDataStream might add additional informations. Therefore in addition to the normal << operator I also tried the writeRawData funkction of QDataStream and the normal write function of QFile. I got the same results with all three ways.

          M 1 Reply Last reply 22 Nov 2016, 13:19
          0
          • M mobo
            22 Nov 2016, 13:12

            Hi mrjj,
            thanks for your answer!
            Yes I read about the possibility that QDataStream might add additional informations. Therefore in addition to the normal << operator I also tried the writeRawData funkction of QDataStream and the normal write function of QFile. I got the same results with all three ways.

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 22 Nov 2016, 13:19 last edited by
            #5

            @mobo
            Hi
            Ok, then something else is wrong.
            Maybe @m-sue answer will help.

            1 Reply Last reply
            1
            • M Offline
              M Offline
              mobo
              wrote on 22 Nov 2016, 13:21 last edited by
              #6

              Thanks m.sue,

              that solved it!
              I deleted the QIODevice::Text in the open function and everything is fine!

              M 1 Reply Last reply 22 Nov 2016, 13:21
              1
              • M mobo
                22 Nov 2016, 13:21

                Thanks m.sue,

                that solved it!
                I deleted the QIODevice::Text in the open function and everything is fine!

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 22 Nov 2016, 13:21 last edited by
                #7

                @mobo
                ok so it was adding newline or something like that?

                1 Reply Last reply
                1
                • M Offline
                  M Offline
                  mobo
                  wrote on 22 Nov 2016, 13:26 last edited by
                  #8

                  Yes, it add CR(0x0D) symbol to LF(0x0A) symbol in Windows.

                  How do I mark this as solved? With the topic tool button I can only delete this topic.. Is that, because I am a newbie here?

                  M 1 Reply Last reply 22 Nov 2016, 13:27
                  0
                  • M mobo
                    22 Nov 2016, 13:26

                    Yes, it add CR(0x0D) symbol to LF(0x0A) symbol in Windows.

                    How do I mark this as solved? With the topic tool button I can only delete this topic.. Is that, because I am a newbie here?

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 22 Nov 2016, 13:27 last edited by mrjj
                    #9

                    @mobo
                    Nope, we loooove newbies here :)

                    You must first select Ask as question and then it can be Solved.
                    (yep its NOT clear )

                    Update:
                    On first post in list.

                    1 Reply Last reply
                    2

                    1/9

                    22 Nov 2016, 12:50

                    • Login

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