Reading and writing files
=========================
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:
- Constructor: ifstream::ifstream ()
Make an `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)
- Constructor: ifstream::ifstream (int FD)
Make an `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.)
- Constructor: ifstream::ifstream (const char* FNAME [, int MODE
[, int PROT]])
Open a file `*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'
Open for input. (Included in ANSI draft.)
`ios::out'
Open for output. (Included in ANSI draft.)
`ios::ate'
Set the initial input (or output) position to the end of the
file.
`ios::app'
Seek to end of file before each write. (Included in ANSI
draft.)
`ios::trunc'
Guarantee a fresh file; discard any contents that were
previously associated with it.
`ios::nocreate'
Guarantee an existing file; fail if the specified file did
not already exist.
`ios::noreplace'
Guarantee a new file; fail if the specified file already
existed.
`ios::bin'
Open as a binary file (on systems where binary and text files
have different properties, typically how `\n' is mapped;
included in ANSI draft).
The last optional argument PROT is specific to Unix-like systems;
it specifies the file protection (by default `0644').
- Method: void ifstream::open (const char* FNAME [, int MODE [, int
PROT]])
Open a file explicitly after the associated `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:
- Constructor: ofstream::ofstream ()
Make an `ofstream' associated with a new file for output.
- Constructor: ofstream::ofstream (int FD)
Make an `ofstream' for writing to a file that was already open,
using file descriptor FD.
- Constructor: ofstream::ofstream (const char* FNAME [, int MODE
[, int PROT]])
Open a file `*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 `0644').
- Destructor: ofstream::~ofstream ()
The files associated with `ofstream' objects are closed when the
corresponding object is destroyed.
- Method: void ofstream::open (const char* FNAME [, int MODE [, int
PROT]])
Open a file explicitly after the associated `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:
- Method: void fstreambase::close ()
Close the file associated with this object, and set `ios::fail' in
this object to mark the event.