2006-09-04 10:26:34 +02:00
|
|
|
The class ti(ostream) supports both formatted and emi(binary output).
|
|
|
|
|
2009-10-22 16:59:49 +02:00
|
|
|
The emi(insertion operator) (lshift()) is used to insert values in
|
|
|
|
a type safe way into tt(ostream) objects. This is called
|
2006-09-04 10:26:34 +02:00
|
|
|
i(formatted output), as binary values which are stored in the computer's
|
|
|
|
memory are converted to human-readable i(ASCII) characters according to
|
|
|
|
certain formatting rules.
|
|
|
|
|
2009-10-22 22:12:51 +02:00
|
|
|
The insertion operator points to the tt(ostream) object to
|
2009-10-22 16:59:49 +02:00
|
|
|
receive the information. The normal associativity of lshift()
|
2006-09-04 10:26:34 +02:00
|
|
|
remains unaltered, so when a statement like
|
|
|
|
verb(
|
|
|
|
cout << "hello " << "world";
|
|
|
|
)
|
|
|
|
is encountered, the leftmost two operands are evaluated first (tt(cout)
|
|
|
|
lshift() tt("hello ")), and an tt(ostream &) object, which is actually the
|
|
|
|
same tt(cout) object, is returned. Now, the statement is reduced to
|
|
|
|
verb(
|
|
|
|
cout << "world";
|
|
|
|
)
|
|
|
|
and the second string is inserted into tt(cout).
|
|
|
|
|
|
|
|
The lshift() operator has a lot of (overloaded) variants, so many types of
|
|
|
|
variables can be inserted into tt(ostream) objects. There is an overloaded
|
|
|
|
lshift()-operator expecting an tt(int), a tt(double), a pointer, etc. etc..
|
2009-10-22 16:59:49 +02:00
|
|
|
Each operator will return the tt(ostream) object into which the information so
|
|
|
|
far has been inserted, followed by the next insertion.
|
2006-09-04 10:26:34 +02:00
|
|
|
|
2009-10-22 16:59:49 +02:00
|
|
|
Streams lack facilities for formatted output like bf(C)'s
|
|
|
|
ti(printf) and ti(vprintf) functions. Although it is not difficult to
|
|
|
|
implement these facilities in the world of streams, tt(printf)-like
|
2007-07-25 11:03:21 +02:00
|
|
|
functionality is hardly ever required in bf(C++) programs. Furthermore, as it
|
|
|
|
is potentially type-em(unsafe), it might be better to avoid this functionality
|
|
|
|
completely.
|
2006-09-04 10:26:34 +02:00
|
|
|
|
2010-02-28 19:58:37 +01:00
|
|
|
When hi(binary file) binary files must be written, normally no
|
2006-09-04 10:26:34 +02:00
|
|
|
text-formatting is used or required: an tt(int) value should be written as a
|
2009-10-22 16:59:49 +02:00
|
|
|
series of raw bytes, not as a series of i(ASCII) numeric characters 0 to
|
2006-09-04 10:26:34 +02:00
|
|
|
9. The following member functions of tt(ostream) objects may be used to
|
|
|
|
write `binary files':
|
|
|
|
itemization(
|
2009-12-28 17:56:23 +01:00
|
|
|
ithtq(put)(ostream& put(char c))
|
2009-10-22 16:59:49 +02:00
|
|
|
(to write a single character to the output stream. Since a character
|
|
|
|
is a byte, this member function could also be used for writing a single
|
2009-12-23 16:30:04 +01:00
|
|
|
character to a text-file.)
|
2009-12-28 17:56:23 +01:00
|
|
|
ithtq(write)(ostream& write(char const *buffer, int length))
|
2009-10-22 16:59:49 +02:00
|
|
|
(to write at most tt(length) bytes, stored in the tt(char const
|
|
|
|
*buffer) to the tt(ostream) object. Bytes are written as they are stored
|
|
|
|
in the buffer, no formatting is done whatsoever. Note that the first argument
|
|
|
|
is a tt(char const *): a em(type cast) is required to write any other
|
|
|
|
type. For example, to write an tt(int) as an unformatted series of
|
|
|
|
byte-values use:
|
2006-09-04 10:26:34 +02:00
|
|
|
verb(
|
|
|
|
int x;
|
|
|
|
out.write(reinterpret_cast<char const *>(&x), sizeof(int));
|
|
|
|
)
|
|
|
|
)
|
2009-12-23 16:30:04 +01:00
|
|
|
The bytes written by the above tt(write) call will be written in an order
|
|
|
|
depending on the emi(endian)em(-ness) of the underlying hardware. Big-endian
|
|
|
|
computers will write the most significant byte first, little-endian computers
|
|
|
|
will first write the least significant byte.
|
2009-10-25 17:44:33 +01:00
|
|
|
)
|