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. Writing bytes to a file
Forum Update on Monday, May 27th 2025

Writing bytes to a file

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 3.7k 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
    mulfycrowh
    wrote on 4 Aug 2016, 17:25 last edited by
    #1

    Hello everybody.

    Here is my code:

    	QString filename = m_outputfolder;
    	filename = filename.append("/");
    	filename = filename.append(m_database->m_reference);
    	filename = filename.append(ext_output);
    	QFile file(filename);
    	file.open(QIODevice::WriteOnly);
    	QDataStream out(&file);
    	out << header;
    	out << msg_out.toUtf8();
    	out << m_database->m_operator.toUtf8();
    	out << separator;
    	out << m_database->m_operatorDates.toUtf8();
    	out << separator;
    	out << m_database->m_period.toUtf8();
    	out << separator;
    	out << m_database->m_duration.toUtf8();
    	file.close();
    

    header is defined :

    static const qint8 header = (qint8) 0xF6;
    

    separator :

    static const qint8 separator = (qint8) 0x09;
    

    The trouble comes with the file because I get extra bytes, always 4 bytes that I do not want.
    Here is the file:

    f6 00 00 00 09 ....... (for msg_out) 00 00 00 0e ....... (for m_operator) 09 00 00 00 17 ..... (for m_operatorDates) 09
    00 00 00 09 ...... (for m_period) 09 00 00 00 08 ......(for m_duration) END OF FILE

    As you can see extra bytes are written, always a group of 4 bytes.
    Could you help please ?
    Thanks

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on 4 Aug 2016, 17:45 last edited by
      #2

      Hi! The stream gets encoded to allow platform independence. What the actual binary representation looks like depends on the used data serialization format. If you don't want this you can also read/write raw unencoded binary data.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mulfycrowh
        wrote on 4 Aug 2016, 19:56 last edited by
        #3

        OK. Is it possible the 4 bytes while using << ?

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mulfycrowh
          wrote on 4 Aug 2016, 20:04 last edited by
          #4

          I mean remove the 4 bytes

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on 4 Aug 2016, 20:12 last edited by
            #5

            Yes, see: read/write raw unencoded binary data.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mulfycrowh
              wrote on 4 Aug 2016, 20:42 last edited by
              #6

              I tried to write a QString using writeRawData.
              I wrote:

              QString stow = stringToWrite.toUtf8();
              const char* ctow = stow.toStdString().c_str();
              ...
              out.writeRawData(ctow, stow.length());
              

              The trouble is that, in the file, the first byte is wrong. It is set to 00.

              1 Reply Last reply
              0
              • R Offline
                R Offline
                Rondog
                wrote on 4 Aug 2016, 21:11 last edited by
                #7

                When reading or writing binary files I find it a lot easier to deal with the data directly in a QByteArray. I have used QDataStream in some cases but most of the time the files you are trying to read are foreign to Qt and could be formatted in a variety of ways.

                QByteArray file_data;
                file_data = file.readAll();
                file.close();
                // extract the information needed
                

                The only part about this is when you need to deal with values (integer, double, variable sized strings, ...). I wrote a utility class that allows me to extract or place the value within the file data directly. For example, a 32 bit integer that is little endian will requires 4 bytes so a function is needed that takes an index and the integer value and it handles placing it properly into the QByteArray. When done it is easy to write:

                QByteArray file_data;
                // populate the file_data as needed
                file_data = file.write(file_data);
                file.close();
                

                The one thing about binary files is usually they are well contained and predicable particular if it contains numerical data (if all the values are double precision then they are aligned to 8 byte boundaries). Binary files containing mostly strings are a little harder as you end up walking through the data from start to finish (which you end up doing anyway with a QDataStream).

                Just a thought.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mulfycrowh
                  wrote on 4 Aug 2016, 21:26 last edited by
                  #8

                  I succeeded in using toUtf8() applied to the QString.It gives a QByteArray and then apply data() to get the const char*.

                  1 Reply Last reply
                  0

                  3/8

                  4 Aug 2016, 19:56

                  5 unread
                  • Login

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