Re: portable handling of binary data
On Dec 18, 1:55 pm, SG <s.gesem...@gmail.com> wrote:
On 17 Dez., 14:24, James Kanze <james.ka...@gmail.com> wrote:
All "raw IO" in C++ is defined in terms of char. But I
don't really see any advantage of read() over using
istream::get(), as above, and I see several (very minor)
disadvantages.
You mean
int istream::get();
Yes.
But I'd like to be able to "decode" such 16bit and 32bit ints
from raw memory instead of having to use an istream object.
Well, you can always design a streambuf to do it. But why? The
only reason for serialization is IO.
As far as I can tell
istream& istream::get(char* s, streamsize n);
is useless on binary data as it only reads until a delimiter
('\n') is found whereas
Yes.
istream& istream::read(char* s, streamsize n);
behaves exactly like I need it.
If you know in advance that there will be enough bytes in the
stream (or it is a format error). In practice, with most
protocols, you can't use it for more than about four bytes
anyway, and the bufferization of the stream means that there
isn't really any difference in spead compared to using
istream::get().
I also suspect that calling istream::get() for every single
byte might hurt the performance -- and no, I havn't tested it.
I just don't see any reason to do it that way.
The main reason is that it is a lot more convenient, and more
natural.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34