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. the difference between QDataStream and QFile
Forum Updated to NodeBB v4.3 + New Features

the difference between QDataStream and QFile

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 298 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.
  • J Offline
    J Offline
    jgxy1123
    wrote on last edited by
    #1

    I have a problem with QTcpSocket.

    1. read the all bytes from the source file of the server( for example, 10000bytes)
    2. send these bytes to the client by QTcpSocket of QTcpServer
    3. QTcpSocket of the licent can receive these bytes correctly,10000bytes.
    4. create a file in the client and open with write mode.
    5. create a QDataStream and binds it with the file.
    6. write these bytes to the file with the QDataStream

    And the problem is the final number of bytes written to the file will be a little more than 10000bytes. If I use QFile::Write() directly , there will be no problem.

    I don't know why there is the difference of the bytes written between QDataStream::<< and QFile::Write()

    jsulmJ 1 Reply Last reply
    0
    • J jgxy1123

      I have a problem with QTcpSocket.

      1. read the all bytes from the source file of the server( for example, 10000bytes)
      2. send these bytes to the client by QTcpSocket of QTcpServer
      3. QTcpSocket of the licent can receive these bytes correctly,10000bytes.
      4. create a file in the client and open with write mode.
      5. create a QDataStream and binds it with the file.
      6. write these bytes to the file with the QDataStream

      And the problem is the final number of bytes written to the file will be a little more than 10000bytes. If I use QFile::Write() directly , there will be no problem.

      I don't know why there is the difference of the bytes written between QDataStream::<< and QFile::Write()

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @jgxy1123 If you just want to write a byte stream into a file there is no need to use QDataStream.
      QDataStream is used to serialise/deserialise data structures, that's also why some additional data is written (for example the version of the format).

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • Paul ColbyP Offline
        Paul ColbyP Offline
        Paul Colby
        wrote on last edited by
        #3

        Hi @jgxy1123,

        And the problem is the final number of bytes written to the file will be a little more than 10000bytes.

        Which is correct - it should be more than the 10000 bytes of data.

        As @jsulm indicated, and per the docs, QDataStream is not raw data, but rather:

        A data stream is a binary stream of encoded information which is 100% independent of the host computer's operating system, CPU or byte order.
        ...
        To take one example, a char * string is written as a 32-bit integer equal to the length of the string including the \0 byte, followed by all the characters of the string including the \0 byte. When reading a char * string, 4 bytes are read to create the 32-bit length value, then that many characters for the char * string including the \0 terminator are read.

        The exact format will depend on which QDataStream::operator<<() overload is being chosen by the compiler based on your actual code, but the the resulting stream will include things like length indicators, and, possibly have its endianness reversed, depending on the host CPU, etc.

        Cheers.

        1 Reply Last reply
        3

        • Login

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