Bernhard Hidding said:
Now this seems to be complicated. I am doing
ofstream scriptfile;
scriptfile.open(script.c_str(),ios::trunc);
/* here comes my ascii output */
scriptfile.seekp(-1, std::ios_base::cur);
scriptfile.close();
This works fine. However, I have opened an ascii output, not a binary
output, have I?
First, note that C++ does not define 'ASCII'. C++ i/o can
be done in one of two 'modes': 'text' or 'binary'.
What could go wrong when I am not in binary mode?
Unwanted character translations, or not getting a guarantee
of a given behavior which requires binary mode (such as with
'seeking'). 'Seeking' is done using 'character positions'.
On those implementations where 'text' mode translates e.g.
a '\n' from a single character to more than one (such as
Windows), this will skew the actual seek position within a
physical file.
You know I want my code to be robust.
'binary' mode will guarantee that the byte values you write will
not be modified by the OS. (whereas with 'text' mode this can
happen, e.g. with '\n'). Since all ASCII values are a subset
of the range of type 'char', 'binary' mode will preserve them
(but then you'll be responsible for ensuring that proper translation
of e.g. '\n' (if necessary) does occur.
-Mike