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. QDataStream writeRawData question
Forum Update on Monday, May 27th 2025

QDataStream writeRawData question

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 4.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
    kkbear
    wrote on last edited by
    #1

    will the output of writeRawData filled with non-relevant data if the writeRawData size is larger than the QByteArray size??(as the QByteArray is just pointer and the memory location after 5 byte of the pointer might be other data?)
    do we need to fill the rest byte with '\0' manually or QT will do it automatically?

    @QByteArray ba("hello");
    QFile test("test.txt");
    QDataStream ds(&test);
    ds->writeRawData(ba,8);@

    1 Reply Last reply
    0
    • ? This user is from outside of this forum
      ? This user is from outside of this forum
      Guest
      wrote on last edited by
      #2

      Why would you write more data than you actually have to begin with? Instead of a number you can use a method to return the size of what you want to write.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kkbear
        wrote on last edited by
        #3

        because i am writing a file with fixed structure but dynamic input by user
        for example

        username(32byte char[])mypassword (32byte char[])

        i need to make sure i save no more then the user input(which varied from 0 to 32bytes)
        and if i write number of bytes dynamically, i have no way to know how many byte i have to read.

        1 Reply Last reply
        0
        • ? This user is from outside of this forum
          ? This user is from outside of this forum
          Guest
          wrote on last edited by
          #4

          Read until you hit a \0, the rest will be garbage.

          As I tagged your question, the behavior seems unspecified, not only you could have anything in those extra bytes, but your entire output can potentially get corrupted. What I mean is when I tested that, mostly I got extra garbage after the string, but occasional I got Chinese hieroglyphs instead of the string, which was really odd.

          BTW your code is not really working, you still need to open the file for writing, and also ds is not a pointer, so you have to use the . syntax.

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

            Why not use the operator<< overloads or, better, use real C++ objects for storing your data (e.g. use a QString or a QByteArray instead of the char array).

            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