[Interest] QDataStream null terminated string / Bug or expected behavior?

Etienne Sandré-Chardonnal etienne.sandre at m4x.org
Fri Feb 10 16:21:52 CET 2017


I was not comparing directly, but through a QSet<QByteArray>, which was
initially populated using string litterals. So I inserted "test" but after
reading from the stream I had "test\0"

2017-02-09 22:00 GMT+01:00 Mike Chinander <chinander at gmail.com>:

> How are you comparing it to "test" after reading to the QByteArray? You
> could use str.data() instead str.c_str(). Or use QStrings to read to/from
> the QDatastreams.
>
> On Thu, Feb 9, 2017 at 11:18 AM, Etienne Sandré-Chardonnal <
> etienne.sandre at m4x.org> wrote:
>
>> Dear all,
>>
>> I just found out a bug in my code which was due to the way I wrote/read
>> from QDataStream.
>>
>> I write a string like that in one program, from a std::string. This uses
>> the null-terminated char* overload of << :
>>
>> std::string str("test");
>> stream << str.c_str();
>>
>> I read the data like that in another program:
>>
>> QByteArray ba;
>> stream >> ba;
>>
>> Then ba.size() returns 5 and it contains the null character. Therefore,
>> comparing it to "test" failed.
>>
>> In the documentation I cannot find a QByteArray & overload of operator
>> >>. QByteArray is not supposed to be castable to char*. What is happening
>> here?
>>
>> Thanks for your help!
>>
>> Best regards,
>>
>> Etienne
>>
>> _______________________________________________
>> Interest mailing list
>> Interest at qt-project.org
>> http://lists.qt-project.org/mailman/listinfo/interest
>>
>>
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20170210/e10c9ca1/attachment.html>


More information about the Interest mailing list