There are two very common special cases of input and output: using files, and using strings in memory.
libio defines four specialized classes for these cases:
ifstream
ofstream
istrstream
ostrstream
These methods are declared in `fstream.h'.
You can read data from class ifstream with any operation from class
istream. There are also a few specialized facilities:
ifstream associated with a new file for input. (If you
use this version of the constructor, you need to call
ifstream::open before actually reading anything)
ifstream for reading from a file that was already open,
using file descriptor fd. (This constructor is compatible with
other versions of iostreams for POSIX systems, but is not part of
the ANSI working paper.)
*fname for this ifstream object.
By default, the file is opened for input (with ios::in as
mode). If you use this constructor, the file will be closed when
the ifstream is destroyed.
You can use the optional argument mode to specify how to open the
file, by combining these enumerated values (with `|' bitwise or).
(These values are actually defined in class ios, so that all
file-related streams may inherit them.) Only some of these modes are
defined in the latest draft ANSI specification; if portability is
important, you may wish to avoid the others.
ios::in
ios::out
ios::ate
ios::app
ios::trunc
ios::nocreate
ios::noreplace
ios::bin
The last optional argument prot is specific to Unix-like systems; it specifies the file protection (by default `644').
ifstream object
already exists (for instance, after using the default constructor). The
arguments, options and defaults all have the same meanings as in the
fully specified ifstream constructor.
You can write data to class ofstream with any operation from class
ostream. There are also a few specialized facilities:
ofstream associated with a new file for output.
ofstream for writing to a file that was already open,
using file descriptor fd.
*fname for this ofstream object.
By default, the file is opened for output (with ios::out as mode).
You can use the optional argument mode to specify how to open the
file, just as described for ifstream::ifstream.
The last optional argument prot specifies the file protection (by default `644').
ofstream objects are closed when the
corresponding object is destroyed.
ofstream object
already exists (for instance, after using the default constructor). The
arguments, options and defaults all have the same meanings as in the
fully specified ofstream constructor.
The class fstream combines the facilities of ifstream and
ofstream, just as iostream combines istream and
ostream.
The class fstreambase underlies both ifstream and
ofstream. They both inherit this additional method:
ios::fail in
this object to mark the event.
The classes istrstream, ostrstream, and strstream
provide some additional features for reading and writing strings in
memory--both static strings, and dynamically allocated strings. The
underlying class strstreambase provides some features common to
all three; strstreambuf underlies that in turn.
istrstream with an existing
static string starting at str, of size size. If you do not
specify size, the string is treated as a NUL terminated string.
ifstream::ifstream; if you do not specify
one, the new stream is simply open for output, with mode ios::out.
ostrstream.
ostrstream. Implies
`ostrstream::freeze()'.
Note that if you want the string to be nul-terminated, you must do that yourself (perhaps by writing ends to the stream).
ostrstream is not to change dynamically; while frozen,
it will not be reallocated if it needs more space, and it will not be
deallocated when the ostrstream is destroyed. Use
`freeze(1)' if you refer to the string as a pointer after creating
it via ostrstream facilities.
`freeze(0)' cancels this declaration, allowing a dynamically
allocated string to be freed when its ostrstream is destroyed.
If this ostrstream is already static--that is, if it was created
to manage an existing statically allocated string---freeze is
unnecessary, and has no effect.
freeze(1) is in effect for this string.
strstreambuf.
Go to the first, previous, next, last section, table of contents.