Important: Please read the Qt Code of Conduct -

QDataStream writeRawData question

  • 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);

  • 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.

  • 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.

  • 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.

  • 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).

Log in to reply