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

Mike Chinander chinander at gmail.com
Thu Feb 9 22:00:02 CET 2017


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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20170209/49e89505/attachment.html>


More information about the Interest mailing list