On Jul 25, 9:06 am, Ondra Holub <ondra.ho...@post.czwrote:
On 25 ec, 08:51, masood.iq...@lycos.com wrote:
Data are read from/written to binary files as they are. There
is no change. In text files may be done some conversions.
It's a bit more subtle than that. Especially with the standard
streams, which do code translation using the codecvt facet of
the imbued locale regardless of whether the file is binary or
text.
Text mode only guarantees textual integrity: you're only
guaranteed to read what you've written if what you've written
consisted only in printable characters, and even then, there are
exceptions. (You're not guaranteed to be able to read trailing
white space, for example. And it's not specified what happens
if the last character written wasn't a '\n'.) On the other
hand, you're guaranteed that a '\n' will result in whatever the
system normally uses as a line separator (e.g. the two character
sequence 0x0D, 0x0A under Windows, or a new record on systems
with record oriented files). And that there are no extra
characters at the end. Also, you can only seek in a limited
number of cases. In binary mode, you'll also get the bytes you
wrote. All of them, not just printable characters. You can
legally write anything, and will reread exactly what you have
written; '\n' will result in one byte being written, with
whatever the encoding of '\n' is on your system. And you can
seek anyway. But you might read extra 0's that you didn't write
at the end of the file.
Also, on some systems, files written in text mode cannot be read
in binary, and vice versa.
It depends on paltform. For example on DOS/Windows platform it
translates line nedings to CRLF characters (and vice versa).
As far as I know there may be made more translation on some
platforms, although I am not able to give any example.
Even on DOS/Windows, a 0x1A in a text input stream is treated as
EOF, and you won't see anything else after it.
operator >is used for reading value in text form, however read
method is for reading of value in binary form (same applies to
operator>and write method).
Operator >formats, as text. Regardless of file mode. Read
extracts char's from the stream, regardless of file mode. I
regularly use >on files opened in binary mode, and there are
cases where it is reasonable to use read on files opened in text
mode.
Usualy is better to use text form,
because it is more portable among different platforms.
It depends what you mean by "portable". If you're writing files
to be read on the same system, or reading files that were
written as text on the same system, text mode gives you a larger
degree of source code portability; a new line will always be the
single character '\n', regardless of how it is represented on
the system. If you're writing files that will be read by many
different systems, you should define a "portable" format for
them, which most likely will require that they be accessed in
binary mode.
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34