|
|
#include <fstream.h>typedef long streamoff, streampos; class ios { public: enum seek_dir { beg, cur, end }; enum open_mode { in, out, ate, app, trunc, nocreate, noreplace }; enum io_state { goodbit=0, eofbit, failbit, badbit }; // and lots of other stuff, see ios(C++) ... } ;
class ifstream : istream { ifstream() ; ~ifstream() ; ifstream(const char* name, int =ios::in, int prot =filebuf::openprot) ; ifstream(int fd) ; ifstream(int fd, char* p, int l) ;
void attach(int fd) ; int detach() ; void close() ; void open(char* name, int =ios::in, int prot=filebuf::openprot) ; filebuf* rdbuf() ; void setbuf(char* p, int l) ; };
class ofstream : ostream { ofstream() ; ~ofstream() ; ofstream(const char* name, int =ios::out, int prot =filebuf::openprot) ; ofstream(int fd) ; ofstream(int fd, char* p, int l) ;
void attach(int fd) ; int detach() ; void close() ; void open(char* name, int =ios::out, int prot=filebuf::openprot) ; filebuf* rdbuf() ; void setbuf(char* p, int l) ; };
class fstream : iostream { fstream() ; ~fstream() ; fstream(const char* name, int mode, int prot =filebuf::openprot) ; fstream(int fd) ; fstream(int fd, char* p, int l) ;void attach(int fd) ; int detach(); void close() ; void open(char* name, int mode, int prot=filebuf::openprot) ; filebuf* rdbuf() ; void setbuf(char* p, int l) ; };
ifstream
, ofstream
, and fstream
specialize istream
, ostream
, and iostream
,
respectively, to files.
That is, the associated streambuf
will be a filebuf
.
In the following descriptions, assume
ifstream
, ofstream
, or fstream
filebuf*
streambuf*
char*
s
int
s
int
representing an open_mode
stream
, where x is either
``if'', ``of'', or ``f'', are:
stream()
stream
.
stream(
name,
mode,
prot)
stream
and opens file name
using mode as the open mode and prot as the protection mode.
By default, prot is filebuf::openprot
, which is 0644.
The error state (io_state
) of the constructed xstream
will indicate failure in case the open
fails.
stream(
d)
stream
connected to file descriptor d,
which must be already open.
stream(
d,
ptr,
len)
stream
connected to file descriptor d,
and, in addition, initializes
the associated filebuf
to use the len bytes
at ptr as the reserve area.
If ptr is null or len is 0, the filebuf
will be unbuffered.
.attach(
d)
ios::failbit
in f's error state.
.detach()
.close()
filebuf
and thereby breaks the connection
of the f to a file.
f's error state is cleared except on failure.
A failure occurs when the call to f.rdbuf()->close()
fails.
.open(
name,
mode,
prot)
ios::nocreate
is set.
By default, prot is filebuf::openprot
, which is 0644.
Failure occurs if f is already open,
or the call to f.rdbuf()->open()
fails.
ios::failbit
is set in f's error status on failure.
The members of open_mode
are bits that may be or'ed together.
(Because the or'ing returns an int
, open()
takes an
int
rather than an open_mode
argument.)
The meanings of these bits in mode are:
ios::app
ios::app
implies ios::out
.
ios::ate
open()
.
ios::ate
does not imply ios::out
.
ios::in
ios::in
is implied by construction and opens of ifstream
s.
For fstream
s it indicates that input operations should be
allowed if possible.
It is legal to include ios::in
in the modes
of an ostream
in which case it implies that the original
file (if it exists) should not be truncated.
If the file being opened for input does not exist, the open will
fail.
ios::out
ios::out
is implied by construction and opens of ofstream
s.
For fstream
it says that output operations are to
be allowed.
ios::out
may be specified even if prot
does not permit output.
ios::trunc
ios::out
is specified
(including implicit specification for ofstream
)
and neither ios::ate
nor ios::app
is specified.
ios::nocreate
open()
will fail.
ios::noreplace
open()
will fail.
Only valid with ios::out
.
=
f.rdbuf()
filebuf
associated with f.
fstream::rdbuf()
has the same meaning as
iostream::rdbuf()
but is typed differently.
.setbuf(
p,
len)
setbuf()
(see
filebuf(C++)),
offering space for a reserve area or requesting unbuffered I/O.
Normally the returned psb is f.rdbuf()
, but it is 0
on failure.
A failure occurs if f is open or the call to f.rdbuf()->setbuf
fails.