Re: writing binary files
On 28 Jan, 00:15, =D6=F6 Tiib <oot...@hot.ee> wrote:
On Jan 28, 12:40 am, James Kanze <james.ka...@gmail.com> wrote:
[...]
All that matters is that you document exactly what you've
output, so that people who have to read the data later have a
fighting chance at doing so. But it's still a lot easier for
all concerned to use a portable format, so that upgrading to a
larger machine doesn't mean having to write a special program
just to be able to read the data.
Classical STL that we discuss here contains nothing to produce
portable formats.
Portable in what sense? The classical iostream generate textual
output, designed for humans, and are not designed for round-trip
output and input. (They can be used for such, but you need to
do some addional work.) The issue here (since the original
question concerned binary) was writing something that would be
read by a machine later. It can be done using the << and the >>
operators, but as I say, it requires some additional work. And
it still must be documented.
That is why i have not used non-enwrapped streams for several
years. Actually it is not that bad with floats, doubles and
various ints.
I use them all the time for human readable output. I also use
ostream for some machine readable formats, like XML. (But
floats and doubles require additional care with those, since the
default precision doesn't guarantee a round trip.)
Bigger troublemakers are usually enums,
Enums are easy: just define appropriate << and >> operators.
whole POD structs and most ironically the chars. Since chars
are so bad the problem may arise to construct that fstream
with correct file name on all platforms to support.
I just use UTF-8, and let it go at that. The problem isn't so
much char's, per se, but what we do with them. There's no such
thing as a portable filename, for example, and if the error
messages are in French, some Americans (and others) might have
trouble understanding them.
--
James Kanze