The input/output stream classes provide program access to the file system. In addition, various options for formatting of output and reading of input are provided.
The following diagram shows the hierarchy of the I/O classes:
These classes are associated with the buffer classes shown below:
This chapter also discusses the manipulators that can be used to do such things as format input and output.
fstream.h
streambuf
The filebuf class is derived from the streambuf class, and provides additional functionality required to communicate with external files. Seek operations are supported when the underlying file supports seeking. Both input and output operations may be performed using a filebuf object, again when the underlying file supports read/write access.
filebuf objects are buffered by default, so the reserve area is allocated automatically, unless one is specified when the filebuf object is created. The get area and put area pointers operate as if they were tied together. There's only one current position in a filebuf object.
The filebuf class allows only the get area or the put area, but not both, to be active at a time. This follows from the capability of files opened for both reading and writing to have operations of each type performed at arbitrary locations in the file:
When the get area is empty and a read is done, the underflow() virtual member function reads more characters and fills the get area again. When the put area is full and a write is done, the overflow() virtual member function writes the characters and makes the put area empty again.
C++ programmers who wish to use files without deriving new objects don't need to explicitly create or use a filebuf object.
The following data member is declared in the public interface. Its value is the default file protection that's used when creating new files. It's primarily referenced as a default argument in member functions.
static int const openprot;
The following member functions are declared in the public interface:
filebuf(); filebuf( filedesc ); filebuf( filedesc, char *, int ); ~filebuf(); int is_open() const; filedesc fd() const; filebuf *attach( filedesc ); filebuf *open( char const *, ios::openmode, int = filebuf::openprot ); filebuf *close(); virtual int pbackfail( int ); virtual int overflow( int = EOF ); virtual int underflow(); virtual streambuf *setbuf( char *, int ); virtual streampos seekoff( streamoff, ios::seekdir, ios::openmode ); virtual int sync();
fstreambase class description, streambuf class description
attach a filebuf object to an open file
#include <fstream.h> public: filebuf *filebuf::attach( filedesc hdl );
The attach() public member function connects an existing filebuf object to an open file via the file's descriptor or handle specified by hdl. If the filebuf object is already connected to a file, this function fails. Otherwise, the attach() public member function extracts information from the file system to determine the capabilities of the file, and hence the filebuf object.
The attach() public member function returns a pointer to the filebuf object on success, otherwise it returns NULL.
filebuf::filebuf(), filebuf::fd(), filebuf::open()
disconnect the filebuf object from a file
#include <fstream.h> public: filebuf *filebuf::close();
The close() public member function disconnects the filebuf object from a connected file, and closes the file. Any buffered output is flushed before the file is closed.
The close() public member function returns a pointer to the filebuf object on success, otherwise NULL is returned.
filebuf::filebuf(), filebuf::fd(), filebuf::is_open()
get the file descriptor connected to the filebuf object
#include <fstream.h> public: filedesc filebuf::fd() const;
The fd() public member function queries the state of the filebuf object file handle.
The fd() public member function returns the file descriptor or handle of the file to which the filebuf object is currently connected. If the filebuf object isn't currently connected to a file, EOF is returned.
filebuf::attach(), filebuf::is_open()
create a filebuf object
#include <fstream.h> public: filebuf::filebuf(); filebuf::filebuf( filedesc hdl ); filebuf::filebuf( filedesc hdl, char *buf, int len );
There are three forms of the public filebuf constructor:
This form of the public filebuf constructor is similar to using the default constructor, and calling the attach() member function. A call to the fd() member function for this created filebuf object returns hdl.
This form of the public filebuf constructor is similar to using the default constructor, and calling the attach() and setbuf() member functions.
The public filebuf constructor creates a filebuf object.
filebuf::~filebuf(), filebuf::attach(), filebuf::open(), filebuf::setbuf()
destroy a filebuf object
#include <fstream.h> public: filebuf::~filebuf();
The public filebuf destructor closes the file if it was explicitly opened using the open() member function. Otherwise, the destructor takes no explicit action. The streambuf destructor is called to destroy that portion of the filebuf object.
The call to the public filebuf destructor is inserted implicitly by the compiler at the point where the filebuf object goes out of scope.
The filebuf object is destroyed.
filebuf::filebuf(), filebuf::close()
query the filebuf object's state
#include <fstream.h> public: int filebuf::is_open();
The is_open() public member function queries the filebuf object state.
The is_open() public member function returns a non-zero value if the filebuf object is currently connected to a file. Otherwise, it returns zero.
filebuf::attach(), filebuf::close(), filebuf::fd(), filebuf::open()
connect the filebuf object to a file, and open it
#include <fstream.h> public: filebuf *filebuf::open( const char *name, ios::openmode mode, int prot = filebuf::openprot );
The open() public member function is used to connect the filebuf object to a file specified by the name parameter. The file is opened using the specified mode. For details about the mode parameter, see the description of ios::openmode. The prot parameter specifies the file protection attributes to use when creating a file.
The open() public member function returns a pointer to the filebuf object on success, otherwise NULL is returned.
filebuf::filebuf(), filebuf::close(), filebuf::is_open(), filebuf::openprot
default file protection
#include <fstream.h> public: static int const filebuf::openprot;
The openprot public member data is used to specify the default file protection to be used when creating new files. This value is used as the default if no user-specified value is provided.
The default value is octal 0644. This is generally interpreted as follows:
Note that not all operating systems support all bits.
filebuf::filebuf(), filebuf::open()
write data from the put area in the file
#include <fstream.h> public: virtual int filebuf::overflow( int ch = EOF );
The overflow() public virtual member function provides the output communication to the file to which the filebuf object is connected. Member functions in the streambuf class call the overflow() public virtual member function for the derived class when the put area is full.
The overflow() public virtual member function performs the following steps:
The overflow() public virtual member function returns __NOT_EOF on success, otherwise EOF is returned.
streambuf::overflow(), filebuf::underflow()
handle a failed attempt to put back a character
#include <fstream.h> public: virtual int filebuf::pbackfail( int ch );
The pbackfail() public virtual member function handles an attempt to put back a character when there's no room at the beginning of the get area. The pbackfail() public virtual member function first calls the sync() virtual member function to flush the put area, and then it attempts to seek backwards over ch in the associated file.
The pbackfail() public virtual member function returns ch on success, otherwise EOF is returned.
position the filebuf at a given offset
#include <fstream.h> public: virtual streampos filebuf::seekoff( streamoff offset, ios::seekdir dir, ios::openmode mode );
The seekoff() public virtual member function is used to position the filebuf object (and hence the file) at a particular offset, so that subsequent input or output operations commence from that point. The offset is specified by the offset and dir parameters.
Since the get area and put area pointers are tied together for the filebuf object, the mode parameter is ignored.
Before the actual seek occurs, the get area and put area of the filebuf object are flushed via the sync() virtual member function. Then, the new position in the file is calculated and the seek takes place.
The dir parameter may be ios::beg, ios::cur, or ios::end, and is interpreted in conjunction with the offset parameter as follows:
If the dir parameter has any other value, or the offset parameter doesn't have an appropriate sign, the seekoff() public virtual member function fails.
The seekoff() public virtual member function returns the new position in the file on success, otherwise EOF is returned.
offer a buffer to the filebuf object
#include <fstream.h> public: virtual streambuf *filebuf::setbuf( char *buf, int len );
The setbuf() public virtual member function is used to offer a buffer, specified by buf and len to the filebuf object. If the buf parameter is NULL or the len is less than or equal to zero, the request is to make the filebuf object unbuffered.
If the filebuf object is already connected to a file and has a buffer, the offer is rejected. In other words, a call to the setbuf() public virtual member function after the filebuf object has started to be used usually fails because the filebuf object has set up a buffer.
If the request is to make the filebuf object unbuffered, the offer succeeds.
If the buf is too small (less than five characters), the offer is rejected. Five characters are required to support the default putback area.
Otherwise, the buf is acceptable and the offer succeeds.
If the offer succeeds, the streambuf::setb() member function is called to set up the pointers to the buffer. The streambuf::setb() member function releases the old buffer (if present), depending on how that buffer was allocated.
Calls to the setbuf() public virtual member function are usually made by a class derived from the fstream class, not directly by a user program.
The setbuf() public virtual member function returns a pointer to the filebuf object on success, otherwise it returns NULL.
synchronize the filebuf object with the external file or device
#include <fstream.h> public: virtual int filebuf::sync();
The sync() public virtual member function synchronizes the filebuf object with the external file or device. If the put area contains characters it's flushed. This leaves the file positioned after the last written character. If the get area contains buffered (unread) characters, the file is backed up to be positioned after the last read character.
The get area and put area never both contain characters. |
The sync() public virtual member function returns __NOT_EOF on success, otherwise it returns EOF.
provide input communication from the file
#include <fstream.h> public: virtual int filebuf::underflow();
The underflow() public virtual member function provides the input communication from the file to which the filebuf object is connected. Member functions in the streambuf class call the underflow() public virtual member function for the derived class when the get area is empty.
The underflow() public virtual member function performs the following steps:
The underflow() public virtual member function returns the first unread character of the get area, on success, otherwise it returns EOF. Note that the get pointer isn't advanced on success.
streambuf::underflow(), filebuf::overflow()
fstream.h
fstreambase, iostream
The fstream class is used to access files for reading and writing. The file can be opened and closed, and read, write and seek operations can be performed.
The fstream class provides very little of its own functionality. It's derived from both the fstreambase and iostream classes. The fstream constructors, destructor and member function provide simplified access to the appropriate equivalents in the base classes.
Of the available I/O stream classes, creating an fstream object is the preferred method of accessing a file for both input and output.
The following public member functions are declared:
fstream(); fstream( char const *, ios::openmode = ios::in|ios::out, int = filebuf::openprot ); fstream( filedesc ); fstream( filedesc, char *, int ); ~fstream(); void open( char const *, ios::openmode = ios::in|ios::out, int = filebuf::openprot );
fstreambase, ifstream, iostream and ofstream class descriptions
create a fstream object
#include <fstream.h> public: fstream::fstream(); fstream::fstream( const char *name, ios::openmode mode = ios::in|ios::out, int prot = filebuf::openprot ); fstream::fstream( filedesc hdl ); fstream::fstream( filedesc hdl, char *buf, int len );
There are four forms of the public fstream constructor:
If the open() fails, ios::failbit and ios::badbit are set in the error state in the inherited ios object.
If the connection to hdl fails, ios::failbit and ios::badbit are set in the error state in the inherited ios object. If the setbuf() fails, ios::failbit is set in the error state in the inherited ios object.
The public fstream constructor creates an fstream object.
filebuf::setbuf(), fstream::~fstream(), fstream::open(), fstreambase::attach(), fstreambase::fd(), ios::iostate, ios::openmode, filebuf::openprot
destroy a fstream object
#include <fstream.h> public: fstream::~fstream();
The public fstream destructor doesn't do anything explicit. The call to this destructor is inserted implicitly by the compiler at the point where the fstream object goes out of scope.
The fstream object is destroyed.
connect the fstream object to a file
#include <fstream.h> public: void fstream::open( const char *name, ios::openmode mode = ios::in|ios::out, int prot = filebuf::openprot );
The open() public member function connects the fstream object to the file specified by the name parameter, using the specified mode and prot parameters. The mode parameter is optional, and usually isn't specified unless additional bits (such as ios::binary or ios::text) are to be specified. The connection is made via the C library open() function.
If the open fails, ios::failbit is set in the error state in the inherited ios object.
fstreambase::attach(), fstreambase::close(), fstreambase::fd(), fstreambase::is_open(), ios::iostate, ios::openmode, filebuf::openprot
fstream.h
ios
ifstream, ofstream, fstream
The fstreambase class is a base class that provides common functionality for the three file-based classes, ifstream, ofstream and fstream. The fstreambase class is derived from the ios class, providing the stream state information, plus it provides member functions for opening and closing files. The actual file manipulation work is performed by the filebuf class.
It isn't intended that fstreambase objects be created. Instead, you should create an ifstream, ofstream or fstream object.
The following member functions are declared in the protected interface:
fstreambase(); fstreambase( char const *, ios::openmode, int = filebuf::openprot ); fstreambase( filedesc ); fstreambase( filedesc, char *, int ); ~fstreambase();
The following member functions are declared in the public interface:
void attach( filedesc ); void close(); filedesc fd() const; int is_open() const; void open( char const *, ios::openmode, int = filebuf::openprot ); filebuf *rdbuf() const; void setbuf( char *, int );
filebuf, fstream, ifstream and ofstream class descriptions
attach the fstreambase object to a file
#include <fstream.h> public: void fstreambase::attach( filedesc hdl );
The attach() public member function connects the fstreambase object to the file specified by the hdl parameter.
If the attach() public member function fails, ios::failbit bit is set in the error state in the inherited ios object. The error state in the inherited ios object is cleared on success.
fstreambase::fd(), fstreambase::is_open(), fstreambase::open(), ios::iostate
disconnect the fstreambase object from its file
#include <fstream.h> public: void fstreambase::close();
The close() public member function disconnects the fstreambase object from the file with which it's associated. If the fstreambase object isn't associated with a file, the close() public member function fails.
If the close() public member function fails, ios::failbit is set in the error state in the inherited ios object.
fstreambase::fd(), fstreambase::is_open(), fstreambase::open(), ios::iostate
create a fstreambase object
#include <fstream.h> protected: fstreambase::fstreambase(); fstreambase::fstreambase( char const *name, ios::openmode mode, int prot = filebuf::openprot ); fstreambase::fstreambase( filedesc hdl ); fstreambase::fstreambase( filedesc hdl, char *buf, int len );
There are four forms of the protected fstreambase constructor:
If the call to open() for the file fails, ios::failbit and ios::badbit are set in the error state in the inherited ios object.
If the attachment to the file fails, ios::failbit and ios::badbit are set in the error state in the inherited ios object.
The protected fstreambase constructor produces an fstreambase object.
filebuf::openprot, fstreambase::~fstreambase(), fstreambase::attach(), fstreambase::open(), fstreambase::setbuf(), ios::iostate, ios::openmode
destroy a fstreambase object
#include <fstream.h> protected: fstreambase::~fstreambase();
The protected fstreambase destructor doesn't do anything explicit. The filebuf object associated with the fstreambase object is embedded within the fstreambase object, so the filebuf destructor is called. The ios destructor is called for that portion of the fstreambase object.
The call to the protected fstreambase destructor is inserted implicitly by the compiler at the point where the fstreambase object goes out of scope.
The fstreambase object is destroyed.
fstreambase::fstreambase(), fstreambase::close()
query the state of the associated file
#include <fstream.h> public: int fstreambase::is_open() const;
The is_open() public member function queries the current state of the file associated with the fstreambase object. Calling the is_open() public member function is equivalent to calling the fd() member function and testing for EOF.
The is_open() public member function returns a non-zero value if the fstreambase object is currently connected to a file, otherwise it returns zero.
fstreambase::attach(), fstreambase::fd(), fstreambase::open()
get the descriptor of the attached file
#include <fstream.h> public: filedesc fstreambase::fd() const;
The fd() public member function returns the file descriptor for the file to which the fstreambase object is connected.
The fd() public member function returns the file descriptor for the file to which the fstreambase object is connected. If the fstreambase object isn't currently connected to a file, EOF is returned.
fstreambase::attach(), fstreambase::is_open(), fstreambase::open()
connect the fstreambase object to a file, and open it
#include <fstream.h> public: void fstreambase::open( const char *name, ios::openmode mode, int prot = filebuf::openprot );
The open() public member function connects the fstreambase object to the file specified by name, using the specified mode and prot. The connection is made via the C library open() function.
If the open() fails, ios::failbit is set in the error state in the inherited ios object. The error state in the inherited ios object is cleared on success.
fstreambase::attach(), fstreambase::close(), fstreambase::fd(), fstreambase::is_open(), ios::iostate, ios::openmode, filebuf::openprot
return the address of the filebuf object
#include <fstream.h> public: filebuf *fstreambase::rdbuf() const;
The rdbuf() public member function returns the address of the filebuf object currently associated with the fstreambase object.
The rdbuf() public member function returns a pointer to the filebuf object currently associated with the fstreambase object. If there's no associated filebuf, NULL is returned.
offer the specfied buffer to the filebuf object
#include <fstream.h> public: void fstreambase::setbuf( char *buf, int len );
The setbuf() public member function offers the specified buffer to the filebuf object associated with the fstreambase object. The filebuf might or might not reject the offer, depending on its state.
If the offer is rejected, ios::failbit is set in the error state in the inherited ios object.
filebuf::setbuf(), ios::iostate
fstream.h
fstreambase, istream
The ifstream class is used to access existing files for reading. Such files can be opened and closed, and read and seek operations can be performed.
The ifstream class provides very little of its own functionality. Derived from both the fstreambase and istream classes, its constructors, destructor and member functions provide simplified access to the appropriate equivalents in those base classes.
Of the available I/O stream classes, creating an ifstream object is the preferred method of accessing a file for input-only operations.
The following public member functions are declared:
ifstream(); ifstream( char const *, ios::openmode = ios::in, int = filebuf::openprot ); ifstream( filedesc ); ifstream( filedesc, char *, int ); ~ifstream(); void open( char const *, ios::openmode = ios::in, int = filebuf::openprot );
fstream, fstreambase, istream and ofstream class descriptions
create a ifstream object
#include <fstream.h> public: ifstream::ifstream(); ifstream::ifstream( const char *name, ios::openmode mode = ios::in, int prot = filebuf::openprot ); ifstream::ifstream( filedesc hdl ); ifstream::ifstream( filedesc hdl, char *buf, int len );
There are four forms of the public ifstream constructor:
If the open() fails, ios::failbit and ios::badbit are set in the error state in the inherited ios object.
If the attachment fails, ios::failbit and ios::badbit are set in the error state in the inherited ios object.
If the connection to hdl fails, ios::failbit and ios::badbit are set in the error state in the inherited ios object. If the call to setbuf() fails, ios::failbit is set in the error state in the inherited ios object.
The public ifstream constructor produces an ifstream object.
filebuf::openprot, fstreambase::attach(), fstreambase::setbuf(), fstreambase::fd(), fstreambase::is_open(), ifstream::~ifstream(), ifstream::open(), ios::iostate, ios::openmode
destroy a ifstream object
#include <fstream.h> public: ifstream::~ifstream();
The public ifstream destructor doesn't do anything explicit. The call to the public ifstream destructor is inserted implicitly by the compiler at the point where the ifstream object goes out of scope.
The ifstream object is destroyed.
connect the ifstream object to a file
#include <fstream.h> public: void ifstream::open( const char *name, ios::openmode mode = ios::in, int prot = filebuf::openprot );
The open() public member function connects the ifstream object to the file specified by the name parameter, using the specified mode and prot parameters. The mode parameter is optional, and usually isn't specified unless additional bits (such as ios::binary or ios::text) are to be specified. The connection is made via the C library open() function.
If the open fails, ios::failbit is set in the error state in the inherited ios object.
filebuf::openprot, fstreambase::attach(), fstreambase::close(), fstreambase::fd(), fstreambase::is_open(), ios::iostate, ios::openmode
iostream.h
istream, ostream
The ios class is used to group together common functionality needed for other derived stream classes. It isn't intended that objects of type ios be created.
This class maintains state information about the stream. (the ios name can be thought of as a short-form for I/O State). Error flags, formatting flags, and values and the connection to the buffers used for the input and output are all maintained by the ios class. No information about the buffer itself is stored in an ios object, merely the pointer to the buffer information.
The following member functions are declared in the protected interface:
ios(); void init( streambuf * ); void setstate( ios::iostate );
The following enumeration typedefs are declared in the public interface:
typedef int iostate; typedef long fmtflags; typedef int openmode; typedef int seekdir;
The following member functions are declared in the public interface:
ios( streambuf * ); virtual ~ios(); ostream *tie() const; ostream *tie( ostream * ); streambuf *rdbuf() const; ios::iostate rdstate() const; ios::iostate clear( ios::iostate = 0 ); int good() const; int bad() const; int eof() const; int fail() const; ios::iostate exceptions( ios::iostate ); ios::iostate exceptions() const; ios::fmtflags setf( ios::fmtflags, ios::fmtflags ); ios::fmtflags setf( ios::fmtflags ); ios::fmtflags unsetf( ios::fmtflags ); ios::fmtflags flags( ios::fmtflags ); ios::fmtflags flags() const; char fill( char ); char fill() const; int precision( int ); int precision() const; int width( int ); int width() const; long &iword( int ); void *&pword( int ); static void sync_with_stdio(); static ios::fmtflags bitalloc(); static int xalloc();
The following member operators are declared in the public interface:
operator void *() const; int operator !() const;
iostream, istream, ostream and streambuf class descriptions
query the state of the ios object's badbit flag
#include <iostream.h> public: int ios::bad() const;
The bad() public member function queries the state of the ios object.
The bad() public member function returns a non-zero value if ios::badbit is set in the error state in the inherited ios object, otherwise zero is returned.
ios::clear(), ios::eof(), ios::fail(), ios::good(), ios::iostate, ios::operator !(), ios::operator void *(), ios::rdstate(), ios::setstate()
allocate a new fmtflag bit
#include <iostream.h> public: static ios::fmtflags ios::bitalloc();
The bitalloc() public static member function is used to allocate a new ios::fmtflags bit for use by user-derived classes.
Because the bitalloc() public static member function manipulates static member data, its behavior isn't tied to any one object but affects the entire class of objects. The value that's returned by the bitalloc() public static member function is valid for all objects of all classes derived from the ios class. No subsequent call to the bitalloc() public static member function will return the same value as a previous call.
The bit value allocated may be used with the member functions that query and affect ios::fmtflags. In particular, the bit can be set with the setf() or flags() member functions, or the setiosflags() manipulator, and reset with the unsetf() or flags() member functions or the resetiosflags() manipulator.
There are two constants defined in iostream.h that indicate the number of bits available when a program starts:
The difference between the bit positions indicates how many bits are available.
The bitalloc() public static member function returns the next available ios::fmtflags bit for use by user-derived classes. If no more bits are available, zero is returned.
ios::flags(), ios::fmtflags, ios::setf(), ios::unsetf(), manipulator resetiosflags(), manipulator setiosflags()
clear the iostate for the object
#include <iostream.h> public: iostate ios::clear( ios::iostate flags = 0 );
The clear() public member function is used to change the current value of ios::iostate in the ios object. ios::iostate is cleared, all bits specified in flags are set.
The clear() public member function returns the previous value of ios::iostate.
ios::bad(), ios::eof(), ios::fail(), ios::good(), ios::iostate, ios::operator !(), ios::operator void *(), ios::rdstate(), ios::setstate()
query the state of the ios object's end-of-file bit
#include <iostream.h> public: int ios::eof() const;
The eof() public member function queries the state of the ios object.
The eof() public member function returns a non-zero value if ios::eofbit is set in the error state in the inherited ios object, otherwise zero is returned.
ios::bad(), ios::clear(), ios::fail(), ios::good(), ios::iostate, ios::rdstate(), ios::setstate()
query and/or set the bits that enable exceptions
#include <iostream.h> public: ios::iostate ios::exceptions() const; ios::iostate ios::exceptions( int enable );
The exceptions() public member function queries and/or sets the bits that control which exceptions are enabled. ios::iostate within the ios object is used to enable and disable exceptions.
When a condition arises that sets a bit in ios::iostate, a check is made to see if the same bit is also set in the exception bits. If so, an exception is thrown. Otherwise, no exception is thrown.
The first form of the exceptions() public member function looks up the current setting of the exception bits. The bit values are those described by ios::iostate.
The second form of the function sets the exceptions bits to those specified in the enable parameter, and returns the current settings.
The exceptions() public member function returns the previous setting of the exception bits.
ios::clear(), ios::iostate, ios::rdstate(), ios::setstate()
query the state of the ios object's badbit and failbit flags
#include <iostream.h> public: int ios::fail() const;
The fail() public member function queries the state of the ios object.
The fail() public member function returns a non-zero value if ios::failbit or ios::badbit is set in the error state in the inherited ios object, otherwise zero is returned.
ios::bad(), ios::clear(), ios::eof(), ios::good(), ios::iostate, ios::operator !(), ios::operator void *(), ios::rdstate(), ios::setstate()
query and/or set the fill character
#include <iostream.h> public: char ios::fill() const; char ios::fill( char fillchar );
The fill() public member function queries and/or sets the fill character used when the size of a formatted object is smaller than the format width specified.
The first form of the fill() public member function looks up the current value of the fill character.
The second form of the function sets the fill character to fillchar.
By default, the fill character is a space.
The fill() public member function returns the previous value of the fill character.
ios::fmtflags, manipulator setfill()
query and/or set the format flags
#include <iostream.h> public: ios::fmtflags ios::flags() const; ios::fmtflags ios::flags( ios::fmtflags setbits );
The flags() public member function is used to query and/or set the value of ios::fmtflags in the ios object.
The first form of the flags() public member function looks up the current ios::fmtflags value.
The second form of the function sets ios::fmtflags to the value specified in the setbits parameter.
The setf() public member function only turns bits on, while the flags() public member function turns some bits on and some bits off. |
The flags() public member function returns the previous ios::fmtflags value.
ios::fmtflags, ios::setf(), ios::unsetf(), manipulator dec(), manipulator hex(), manipulator oct(), manipulator resetiosflags(), manipulator setbase(), manipulator setiosflags()
a set of bits for specifying I/O formats
#include <iostream.h> public: enum fmt_flags { skipws = 0x0001, // skip whitespace left = 0x0002, // align field to left edge right = 0x0004, // align field to right edge internal = 0x0008, // sign at left, value at // right dec = 0x0010, // decimal conversion for // integers oct = 0x0020, // octal conversion for // integers hex = 0x0040, // hexadecimal conversion // for integers showbase = 0x0080, // show dec/octal/hex base // on output showpoint = 0x0100, // show decimal and digits // on output uppercase = 0x0200, // use uppercase for format // characters showpos = 0x0400, // use + for output positive // numbers scientific = 0x0800, // use scientific notation // for output fixed = 0x1000, // use floating notation for // output unitbuf = 0x2000, // flush stream after output stdio = 0x4000, // flush stdout/stderr after // output basefield = dec | oct | hex, adjustfield= left | right | internal, floatfield = scientific | fixed }; typedef long fmtflags;
The type ios::fmt_flags is a set of bits representing methods of:
The type fmtflags represents the same set of bits, but uses a long to represent the values, thereby avoiding problems made possible by the compiler's ability to use smaller types for enumerations.
You should use the type ios::fmtflags. |
The bit values defined by the type fmtflags are set and read by the member functions setf(), unsetf() and flags(), as well as the manipulators setiosflags() and resetiosflags().
Because one field is used to store all of these bits, there are three special values used to mask various groups of bits. These values are named ios::basefield, ios::adjustfield and ios::floatfield, and are discussed with the bits that they're used to mask.
ios::skipws controls whether or not whitespace characters are automatically skipped when using an operator >>() extractor. If ios::skipws is on, any use of the operator >>() extractor skips whitespace characters before inputting the next item. Otherwise, skipping of whitespace characters must be handled by the program.
ios::left, ios::right and ios::internal control the alignment of items written using an operator <<() inserter. These bits are usually used in conjunction with the format width and fill character.
ios::adjustfield can be used to mask the alignment bits returned by the setf(), unsetf() and flags() member functions, and for setting new values to ensure that no other bits are accidentally affected.
When the item to be written is smaller than the format width specified, fill characters are written to occupy the additional space:
If no alignment is specified, ios::right is assumed.
If the item to be written is as big as or bigger than the format width specified, no fill characters are written, and the alignment is ignored.
ios::dec, ios::oct and ios::hex control the base used to format integers being written to the stream, and also control the interpretation of integers being read from the stream.
ios::basefield can be used to mask the base bits returned by the member functions setf(), unsetf() and flags(), and for setting new values to ensure that no other bits are accidentally affected.
When an integer is being read from the stream, these bits control the base used for the interpretation of the digits. If none of these bits is set, the number is interpreted, based on the starting character(s), as given in the following table:
Starting character(s) | Interpretation |
---|---|
0x or 0X | hexadecimal (digits 0123456789, plus the letters abcdef or ABCDEF) |
0 (zero) | octal (digits 01234567) |
other | decimal (digits 0123456789) |
If one of the bits is set, then the prefix isn't necessary, and the number is interpreted according to the bit.
When any one of the integer types is being written to the stream, it can be written in decimal, octal or hexadecimal. If none of these bits is set, ios::dec is assumed.
If ios::dec is set (or assumed), the integer is written in decimal (digits 0123456789). No prefix is included.
If ios::oct is set, the integer is written in octal (digits 01234567). No sign character is written, as the number is treated as an unsigned quantity on conversion to octal.
If ios::hex is set, the integer is written in hexadecimal (digits 0123456789, plus the letters abcdef or ABCDEF, depending on the setting of ios::uppercase). No sign character is written, as the number is treated as an unsigned quantity on conversion to hexadecimal.
ios::showbase controls whether or not integers written to the stream in octal or hexadecimal form have a prefix that indicates the base of the number. If the bit is set, decimal numbers are written without a prefix, octal numbers are written with the prefix 0 (zero), and hexadecimal numbers are written with the prefix 0x or 0X, depending on the setting of ios::uppercase. If the ios::showbase isn't set, no prefixes are written.
ios::showpoint is used to control whether or not the decimal point and trailing zeroes are trimmed when floating-point numbers are written to the stream. If the bit is set, no trimming is done, causing the number to appear with the specified format precision. If the bit isn't set, any trailing zeroes after the decimal point are trimmed, and if not followed by any digits, the decimal point is removed as well.
ios::uppercase is used to force to upper-case all letters used in formatting numbers, including the letter-digits abcdef, the x hexadecimal prefix, and the e used for the exponents in floating-point numbers.
ios::showpos controls whether or not a + is added to the front of positive integers being written to the stream. If the bit is set, the number is positive and the number is being written in decimal, a + is written before the first digit.
ios::scientific and ios::fixed control the form used for writing floating-point numbers to the stream. Floating-point numbers can be written in scientific notation (also called exponential notation) or in fixed-point notation.
ios::floatfield can be used to mask the floating-format bits returned by the member functions setf(), unsetf() and flags(), and for setting new values to ensure that no other bits are accidentally affected.
If ios::scientific is set, the floating-point number is written with a leading - sign (for negative numbers), a digit, a decimal point, more digits, an e (or E if ios::uppercase is set), a + or - sign, and two or three digits representing the exponent. The digit before the decimal isn't zero unless the number is zero. The total number of digits before and after the decimal is equal to the specified format precision. If ios::showpoint isn't set, trimming of the decimal and digits following the decimal might occur.
If ios::fixed is set, the floating-point number is written with a - sign (for negative numbers), at least one digit, the decimal point, and as many digits following the decimal as specified by the format precision. If ios::showpoint isn't set, trimming of the decimal and digits following the decimal might occur.
If neither ios::scientific nor ios::fixed is specified, the floating-point number is formatted using scientific notation provided at least one of the following conditions is met:
Otherwise, fixed-point notation is used.
ios::unitbuf controls whether or not the stream is flushed after each item is written. If the bit is set, every item that's written to the stream is followed by a flush operation, which ensures that the I/O stream buffer associated with the stream is kept empty, immediately transferring the data to its final destination.
ios::stdio controls whether or not the stream is synchronized after each item is written. If the bit is set, every item that's written to the stream causes the stream to be synchronized, which means any input or output buffers are flushed so that an I/O operation performed using C (not C++) I/O behaves in an understandable way. If the output buffer isn't flushed, writing using C++ and then C I/O functions could cause the output from the C functions to appear before the output from the C++ functions, since the characters might be sitting in the C++ output buffer. Similarly, after the C output operations are done, a call should be made to the C library fflush() function on the appropriate stream before resuming C++ output operations.
ios::flags(), ios::setf(), ios::unsetf(), manipulator dec(), manipulator hex(), manipulator oct(), manipulator resetiosflags(), manipulator setbase(), manipulator setiosflags()
check the state of the ios object
#include <iostream.h> public: int ios::good() const;
The good() public member function queries the state of the ios object.
The good() public member function returns a non-zero value if none of ios::iostate is clear, otherwise zero is returned.
ios::bad(), ios::clear(), ios::eof(), ios::fail(), ios::iostate, ios::rdstate(), ios::setstate()
initialize the ios object
#include <iostream.h> protected: void ios::init( streambuf *sb );
The init() public protected member function is used by derived classes to explicitly initialize the ios portion of the derived object, and to associate a streambuf with the ios object. The init() public protected member function performs the following steps:
If sb is NULL, the ios::badbit is set in the error state in the inherited ios object.
ios::ios(), ios::iostate, ios::rdbuf()
create a ios object
#include <iostream.h> protected: ios::ios(); public: ios::ios( streambuf *sb );
There are two forms of the ios constructor:
ios::~ios(), ios::init(), ios::iostate
destroy a ios object
#include <iostream.h> public: virtual ios::~ios();
The public virtual ios destructor destroys an ios object. The call to it is inserted implicitly by the compiler at the point where the ios object goes out of scope.
The ios object is destroyed.
a set of bits that reflects the state of the ios object
#include <iostream.h> public: enum io_state { goodbit = 0x00, // no errors badbit = 0x01, // operation failed, // may not proceed failbit = 0x02, // operation failed, // may proceed eofbit = 0x04 // end of file encountered }; typedef int iostate;
The type ios::io_state is a set of bits representing the current state of the stream. The type iostate represents the same set of bits, but uses an int to represent the values, thereby avoiding problems made possible by the compiler's ability to use smaller types for enumerations.
Use the ios::iostate, not ios::io_state. |
The bit values defined by the type iostate can be read and set by the member functions rdstate() and clear(), and can be used to control exception handling with the member function exceptions().
ios::badbit represents the state where the stream is no longer usable because of some error condition.
ios::failbit represents the state where the previous operation on the stream failed, but the stream is still usable. Subsequent operations on the stream are possible, but the state must be cleared using the clear() member function.
ios::eofbit represents the state where the end-of-file condition has been encountered. The stream may still be used, but the state must be cleared using the clear() member function.
Even though ios::goodbit isn't a bit value (because its value is zero, which has no bits on), it's provided for completeness.
ios::bad(), ios::clear(), ios::eof(), ios::exceptions(), ios::fail(), ios::good(), ios::operator !(), ios::operator void *(), ios::rdstate(), ios::setstate()
return a reference to a long integer
#include <iostream.h> public: long &ios::iword( int index );
The iword() public member function creates a reference to a long int, which may be used to store and retrieve any suitable integer value. The index parameter specifies which long int is to be referenced, and must be obtained from a call to the xalloc() static member function.
The iword() and pword() public member functions return
references to the same storage with a different type. Therefore, each
index obtained from the xalloc() static member function
can be used only for an integer or a pointer, not both.
Since the iword() public member function returns a reference and the ios class can't predict how many such items will be required by a program, it should be assumed that each call to the xalloc() static member function invalidates all previous references returned by the iword() public member function. Therefore, the iword() public member function should be called each time the reference is needed. |
The iword() public member function returns a reference to a long int.
a set of bits that represents ways of opening a stream
#include <iostream.h> public: enum open_mode { in = 0x0001, // open for input out = 0x0002, // open for output atend = 0x0004, // seek to end after opening append = 0x0008, // open for output, append // to the end truncate = 0x0010, // discard contents after // opening nocreate = 0x0020, // open only an existing // file noreplace = 0x0040, // open only a new file text = 0x0080, // open as text file binary = 0x0100, // open as binary file app = append, // synonym ate = atend, // synonym trunc = truncate // synonym }; typedef int openmode;
The type ios::open_mode is a set of bits representing ways of opening a stream. The type openmode represents the same set of bits, but uses an int to represent the values, thereby avoiding problems made possible by the compiler's ability to use smaller types for enumerations.
Use the type ios::openmode. |
The bit values defined by type openmode can be specified in the constructors for stream objects, as well as in various member functions.
ios::in is specified in a stream for which input operations may be performed. ios::out is specified in a stream for which output operations may be performed.
ios::atend and ios::ate are equivalent, and either one is specified for streams that are to be positioned to the end before the first operation takes place. ios::ate is provided for historical purposes and compatibility with other implementations of I/O streams. Note that this bit positions the stream to the end exactly once, when the stream is opened.
ios::append and ios::app are equivalent, and either one is specified for streams that are to be positioned to the end before any and all output operations take place. ios::app is provided for historical purposes and compatibility with other implementations of I/O streams. Note that this bit causes the stream to be positioned to the end before each output operation, while ios::atend causes the stream to be positioned to the end only when first opened.
ios::truncate and ios::trunc are equivalent, and either one is specified for streams that are to be truncated to zero length before the first operation takes place. ios::trunc is provided for historical purposes and compatibility with other implementations of I/O streams.
ios::nocreate is specified if the file must exist before it's opened. If the file doesn't exist, an error occurs.
ios::noreplace is specified if the file must not exist before it's opened. That is, the file must be a new file. If the file exists, an error occurs.
ios::text is specified if the file is to be treated as a text file. A text file is divided into records, and each record is terminated by a new-line character, usually represented as \n. The new-line character is translated into a form that's compatible with the underlying file system's concept of text files. This conversion happens automatically whenever the new-line is written to the file, and the inverse conversion (to the new-line character) happens automatically whenever the end of a record is read from the file system.
ios::binary is specified if the file is to be treated as a binary file. Binary files are streams of characters. No character has a special meaning. No grouping of characters into records is apparent to the program, although the underlying file system might cause such a grouping to occur.
The following default behaviors are defined:
test the error state
#include <iostream.h> public: int ios::operator !() const;
The operator !() public member function tests the error state in the inherited ios object of the ios object.
The operator !() public member function returns a non-zero value if either of ios::failbit or ios::badbit bits is set in the error state in the inherited ios object, otherwise zero is returned.
ios::bad(), ios::clear(), ios::fail(), ios::good(), ios::iostate, ios::operator void *(), ios::rdstate(), ios::setstate()
convert the ios object into a pointer
#include <iostream.h> public: ios::operator void *() const;
The operator void *() public member function converts the ios object into a pointer to void. The actual pointer value returned is meaningless and intended only for comparison with NULL to determine the error state in the inherited ios object of the ios object.
The operator void *() public member function returns a NULL pointer if either of ios::failbit or ios::badbit bits is set in the error state in the inherited ios object, otherwise a non-NULL pointer is returned.
ios::bad(), ios::clear(), ios::fail(), ios::good(), ios::iostate, ios::operator !(), ios::rdstate(), ios::setstate()
query and/or set the format precision
#include <iostream.h> public: int ios::precision() const; int ios::precision( int prec );
The precision() public member function is used to query and/or set the format precision. The format precision is used to control the number of digits of precision used when formatting floating-point numbers. For scientific notation, the format precision describes the total number of digits before and after the decimal point, but not including the exponent. For fixed-point notation, the format precision describes the number of digits after the decimal point.
The first form of the precision() public member function looks up the current format precision.
The second form of the function sets the format precision to prec.
By default, the format precision is six. If prec is specified to be less than zero, the format precision is set to six. Otherwise, the specified format precision is used. For scientific notation, a format precision of zero is treated as a precision of one.
The precision() public member function returns the previous format precision setting.
ios::fmtflags, manipulator setprecision()
create a reference to a pointer
#include <iostream.h> public: void * &ios::pword( int index );
The pword() public member function creates a reference to a void pointer, which may be used to store and retrieve any suitable pointer value. The index parameter specifies which void pointer is to be referenced, and must be obtained from a call to the xalloc() static member function.
The iword() and pword() public member functions return
references to the same storage with a different type. Therefore, each
index obtained from the xalloc() static member function
can be used only for an integer or a pointer, not both.
Since the pword() public member function returns a reference and the ios class can't predict how many such items will be required by a program, it should be assumed that each call to the xalloc() static member function invalidates all previous references returned by the pword() public member function. Therefore, the pword() public member function should be called each time the reference is needed. |
The pword() public member function returns a reference to a void pointer.
get a pointer to the streambuf object
#include <iostream.h> public: streambuf *ios::rdbuf() const;
The rdbuf() public member function looks up the pointer to the streambuf object that maintains the buffer associated with the ios object.
The rdbuf() public member function returns the pointer to the streambuf object associated with the ios object. If there's no associated streambuf object, NULL is returned.
query the value of iostate
#include <iostream.h> public: iostate ios::rdstate() const;
The rdstate() public member function is used to query the current value of ios::iostate in the ios object without modifying it.
The rdstate() public member function returns the current value of ios::iostate.
ios::bad(), ios::clear(), ios::eof(), ios::fail(), ios::good(), ios::iostate, ios::operator !(), ios::operator void *(), ios::setstate()
methods of seeking within a stream
#include <iostream.h> public: enum seek_dir { beg, // seek from beginning cur, // seek from current position end // seek from end }; typedef int seekdir;
The type ios::seek_dir is a set of bits representing different methods of seeking within a stream. The type seekdir represents the same set of bits, but uses an int to represent the values, thereby avoiding problems made possible by the compiler's ability to use smaller types for enumerations.
Use the type ios::seekdir. |
The bit values defined by type seekdir are used by the member functions seekg() and seekp(), as well the seekoff() and seekpos() member functions in classes derived from the streambuf class.
ios::beg causes the seek offset to be interpreted as an offset from the beginning of the stream. The offset is specified as a positive value.
ios::cur causes the seek offset to be interpreted as an offset from the current position of the stream. If the offset is a negative value, the seek is towards the start of the stream. Otherwise, the seek is towards the end of the stream.
ios::end causes the seek offset to be interpreted as an offset from the end of the stream. The offset is specified as a negative value.
set bits in the format flags
#include <iostream.h> public: ios::fmtflags ios::setf( ios::fmtflags onbits ); ios::fmtflags ios::setf( ios::fmtflags setbits, ios::fmtflags mask );
The setf() public member function is used to set bits in ios::fmtflags in the ios object.
The first form is used to turn on the bits that are on in the onbits parameter. (onbits is added to ios::fmtflags with an OR operation).
The second form is used to turn off the bits specified in the mask parameter and turn on the bits specified in the setbits parameter. This form is particularly useful for setting the bits described by the ios::basefield, ios::adjustfield and ios::floatfield values, where only one bit should be on at a time.
Both forms of the setf() public member function return the previous ios::fmtflags value.
ios::fmtflags, ios::setf(), ios::unsetf(), manipulator dec(), manipulator hex(), manipulator oct(), manipulator setbase(), manipulator setiosflags(), manipulator resetiosflags()
set bits in the error state for the object
#include <iostream.h> protected: void ios::setstate( int or_bits );
The setstate() protected member function is provided as a convenience for classes derived from the ios class. It turns on the error state in the inherited ios object bits that are set in the or_bits parameter, and leaves the other error state in the inherited ios object bits unchanged.
The setstate() protected member function sets the bits specified by or_bits in the error state in the inherited ios object.
ios::bad(), ios::clear(), ios::eof(), ios::fail(), ios::good(), ios::iostate, ios::operator !(), ios::operator void *(), ios::rdstate()
an obsolete function
#include <iostream.h> public: static void ios::sync_with_stdio();
The sync_with_stdio() public static member function is obsolete. It's provided for compatibility.
The sync_with_stdio() public static member function has no return value.
query and/or set up a connection to another stream
#include <iostream.h> public: ostream *ios::tie() const; ostream *ios::tie( ostream *ostrm );
The tie() public member function is used to query and/or set up a connection between the ios object and another stream. The connection causes the output stream specified by ostrm to be flushed whenever the ios object is about to read characters from a device or is about to write characters to an output buffer or device.
The first form of the tie() public member function is used to query the current tie.
The second form of the function is used to set the tied stream to ostrm.
Normally, the predefined streams cin and cerr set up ties to cout so that any input from the terminal flushes any buffered output, and any writes to cerr flush cout before the characters are written. cout doesn't set up a tie to cerr because cerr has the flag ios::unitbuf set, so it flushes itself after every write operation.
Both forms of the tie() public member function return the previous tie value.
turn off bits in the format flags for the object
#include <iostream.h> public: ios::fmtflags ios::unsetf( ios::fmtflags offbits );
The unsetf() public member function is used to turn off bits in ios::fmtflags that are set in the offbits parameter. All other bits in ios::fmtflags are unchanged.
The unsetf() public member function returns the old ios::fmtflags value.
ios::fmtflags, ios::setf(), ios::unsetf(), manipulator dec(), manipulator hex(), manipulator oct(), manipulator setbase(), manipulator setiosflags(), manipulator resetiosflags(),
query and/or set the format width
#include <iostream.h> public: int ios::width() const; int ios::width( int wid );
The width() public member function is used to query and/or set the format width used to format the next item. A format width of zero indicates that the item is to be written using exactly the number of positions required. Other values indicate that the item must occupy at least that many positions. If the formatted item is larger than the specified format width, the format width is ignored and the item is formatted using the required number of positions.
The first form of the width() public member function is used to query the format width that's to be used for the next item.
The second form of the function is used to set the format width to wid for the next item to be formatted.
After an item has been formatted, the format width is reset to zero. Therefore, any non-zero format width must be set before each item that's to be formatted.
The width() public member function returns the previous format width.
ios::fmtflags, manipulator setw(), manipulator setwidth()
return an index into a general-purpose array
#include <iostream.h> public: static int ios::xalloc();
The xalloc() public static member function returns an index into an array of items that the program may use for any purpose. Each item can be either a long int or a pointer to void. The index can be used with the iword() and pword() member functions.
Because the xalloc() public static member function manipulates static member data, its behavior isn't tied to any one object, but affects the entire class of objects. The value that's returned by the xalloc() public static member function is valid for all objects of all classes derived from the ios class. No subsequent call to the xalloc() public static member function will return the same value as a previous call.
The xalloc() public static member function returns an index for use with the iword() and pword() member functions.
iostream.h
istream, ostream
fstream, strstream
The iostream class supports the reading and writing of characters from and to the standard input/output devices, usually the keyboard and screen. The iostream class provides formatted conversion of characters to and from other types (for example, integers and floating-point numbers). The associated streambuf class provides the methods for communicating with the actual device, while the iostream class provides the interpretation of the characters.
Generally, an iostream object won't be created by a program, since there's no mechanism at this level to ``open'' a device. No instance of an iostream object is created by default, since it's usually not possible to perform both input and output on the standard input/output devices. The iostream class is provided as a base class for other derived classes that can provide both input and output capabilities through the same object. The fstream and strstream classes are examples of classes derived from the iostream class.
The following protected member functions are declared:
iostream();
The following public member functions are declared:
iostream( ios const & ); iostream( streambuf * ); virtual ~iostream();
The following public member operators are declared:
iostream & operator =( streambuf * ); iostream & operator =( ios const & );
ios, istream and ostream class descriptions
create a iostream object
#include <iostream.h> protected: iostream::iostream(); public: iostream::iostream( ios const &strm ); iostream::iostream( streambuf *sb );
There are three forms of the iostream constructor:
This form of the constructor is only used implicitly by the compiler when it generates a constructor for a derived class.
Since a user program usually won't create an iostream object, this form of the public iostream constructor is unlikely to be explicitly used, except in the member initializer list for the constructor of a derived class. The sb parameter is a pointer to a streambuf object, which should be connected to the source and sink of characters for the stream.
ios::iostate, iostream::~iostream()
destroy a iostream object
#include <iostream.h> public: virtual iostream::~iostream();
The public iostream destructor doesn't do anything explicit. The ios destructor is called for that portion of the iostream object. The call to the public iostream destructor is inserted implicitly by the compiler at the point where the iostream object goes out of scope.
The iostream object is destroyed.
initialize the iostream object
#include <iostream.h> public: iostream &iostream::operator =( streambuf *sb ); iostream &iostream::operator =( const ios &strm );
There are two forms of the operator =() public member function:
The operator =() public member function returns a reference to the iostream object that's the target of the assignment.
iostream.h
ios
iostream, ifstream, istrstream
The istream class supports reading characters from a class derived from streambuf, and provides formatted conversion of characters into other types (such as integers and floating-point numbers). The streambuf class provides the methods for communicating with the external device (keyboard, disk), while the istream class provides the interpretation of the resulting characters.
Generally, an istream object won't be explicitly created by a program, since there's no mechanism at this level to open a device. The only default istream object in a program is cin, which reads from standard input (usually the keyboard).
The istream class supports two basic concepts of input: formatted and unformatted. The overloaded operator >>() member functions are called extractors, and they provide the support for formatted input. The rest of the member functions deal with unformatted input, managing the state of the ios object and providing a friendlier interface to the associated streambuf object.
The following protected member functions are declared:
istream(); eatwhite();
The following public member functions are declared:
istream( istream const & ); istream( streambuf * ); virtual ~istream(); int ipfx( int = 0 ); void isfx(); int get(); istream &get( char *, int, char = '\n' ); istream &get( signed char *, int, char = '\n' ); istream &get( unsigned char *, int, char = '\n' ); istream &get( char & ); istream &get( signed char & ); istream &get( unsigned char & ); istream &get( streambuf &, char = '\n' ); istream &getline( char *, int, char = '\n' ); istream &getline( signed char *, int, char = '\n' ); istream &getline( unsigned char *, int, char = '\n' ); istream &ignore( int = 1, int = EOF ); istream &read( char *, int ); istream &read( signed char *, int ); istream &read( unsigned char *, int ); istream &seekg( streampos ); istream &seekg( streamoff, ios::seekdir ); istream &putback( char ); streampos tellg(); int gcount() const; int peek(); int sync();
The following public member operators are declared:
istream &operator =( streambuf * ); istream &operator =( istream const & ); istream &operator >>( char * ); istream &operator >>( signed char * ); istream &operator >>( unsigned char * ); istream &operator >>( char & ); istream &operator >>( signed char & ); istream &operator >>( unsigned char & ); istream &operator >>( signed short & ); istream &operator >>( unsigned short & ); istream &operator >>( signed int & ); istream &operator >>( unsigned int & ); istream &operator >>( signed long & ); istream &operator >>( unsigned long & ); istream &operator >>( float & ); istream &operator >>( double & ); istream &operator >>( long double & ); istream &operator >>( streambuf & ); istream &operator >>( istream &(*)( istream & ) ); istream &operator >>( ios &(*)( ios & ) );
ios, iostream and ostream class descriptions
discard whitespace characters
#include <iostream.h> protected: void istream::eatwhite();
The eatwhite() protected member function extracts and discards whitespace characters from the istream object until a non-whitespace character is found. The non-whitespace character isn't extracted.
The eatwhite() protected member function sets ios::eofbit in the error state in the inherited ios object if end-of-file is encountered as the first character while extracting whitespace characters.
istream::ignore(), ios::fmtflags, ios::iostate
determine the number of characters most recently extracted
#include <iostream.h> public: int istream::gcount() const;
The gcount() public member function determines the number of characters extracted by the last unformatted input member function.
The gcount() public member function returns the number of characters extracted by the last unformatted input member function.
istream::get(), istream::getline(), istream::read()
perform an unformatted read
#include <iostream.h> public: // return a character int istream::get(); // read a character istream &istream::get( char &ch ); istream &istream::get( signed char &ch ); istream &istream::get( unsigned char &ch ); // read a number of characters istream &istream::get( char *buf, int len, char delim = '\n' ); istream &istream::get( signed char *buf, int len, char delim = '\n' ); istream &istream::get( unsigned char *buf, int len, char delim = '\n' ); // read until a given delimiter is found istream &istream::get( streambuf &sb, char delim = '\n' );
The get() public member function performs an unformatted read. The arguments determine what is read.
This form of the get() public member function performs an unformatted read of a single character from the istream object.
These forms of the get() public member function perform an unformatted read of a single character from the istream object and store the character in the ch parameter.
These forms of the get() public member function perform an unformatted read of at most len -1 characters from the istream object, and store them starting at the memory location specified by the buf parameter. If the character specified by the delim parameter is encountered in the istream object before len -1 characters have been read, the read terminates without extracting the delimiting character.
After the read terminates, whether or not an error occurred, a null character is stored in buf following the last character read from the istream object.
If the delim parameter isn't specified, the new-line character is assumed.
This form of the get() public member function performs an unformatted read of characters from the istream object and transfers them to the streambuf object specified in the sb parameter. The transfer stops if end-of-file is encountered, the delimiting character specified in the delim parameter is found, or if the store into the sb parameter fails. If the delim character is found, it isn't extracted from the istream object and isn't transferred to the sb object.
If the delim parameter isn't specified, the new-line character is assumed.
This form of the get() public member function returns the character read from the istream object. If the istream object is positioned at end-of-file before the read, EOF is returned and ios::eofbit bit is set in the error state in the inherited ios object. ios::failbit bit isn't set by this form of the get() public member function.
These forms of the get() public member function return a reference to the istream object. ios::eofbit is set in the error state in the inherited ios object if the istream object is positioned at end-of-file before the attempt to read the character. ios::failbit is set in the error state in the inherited ios object if no character is read.
These forms of the get() public member function return a reference to the istream object. If end-of-file is encountered as the first character, ios::eofbit is set in the error state in the inherited ios object. If no characters are stored into buf, ios::failbit is set in the error state in the inherited ios object.
This form of the get() public member function returns a reference to the istream object. ios::failbit is set in the error state in the inherited ios object if the store into the streambuf object fails.
istream::getline(), istream::putback(), istream::read(), istream::operator >>(), ios::iostate
read a line of characters
#include <iostream.h> public: istream &istream::getline( char *buf, int len, char delim = '\n' ); istream &istream::getline( signed char *buf, int len, char delim = '\n' ); istream &istream::getline( unsigned char *buf, int len, char delim = '\n' );
The getline() public member function performs an unformatted read of at most len -1 characters from the istream object, and stores them starting at the memory location specified by the buf parameter. If the delimiting character, specified by the delim parameter, is encountered in the istream object before len -1 characters have been read, the read terminates after extracting the delim character.
If len -1 characters have been read and the next character is the delim character, it isn't extracted.
After the read terminates, whether or not an error occurred, a null character is stored in the buffer following the last character read from the istream object.
If the delim parameter isn't specified, the new-line character is assumed.
The getline() public member function returns a reference to the istream object. If end-of-file is encountered as the first character, ios::eofbit is set in the error state in the inherited ios object. If end-of-file is encountered before len characters are transferred or the delim character is reached, ios::failbit is set in the error state in the inherited ios object.
istream::get(), istream::read(), istream::operator >>(), ios::iostate
discard a given number of characters
#include <iostream.h> public: istream &istream::ignore( int num = 1, int delim = EOF );
The ignore() public member function extracts and discards up to num characters from the istream object. If the num parameter isn't specified, this function extracts and discards one character. If the delim parameter isn't EOF and it's encountered before num characters have been extracted, the extraction ceases after discarding the delimiting character. The extraction stops if end-of-file is encountered.
If the num parameter is specified as a negative number, no limit is imposed on the number of characters extracted and discarded. The operation continues until the delimiting character is found and discarded, or until end-of-file. This behavior is a WATCOM extension.
The ignore() public member function returns a reference to the istream object. If end-of-file is encountered as the first character, ios::eofbit is set in the error state in the inherited ios object.
istream::eatwhite(), ios::iostate
a prefix function that's executed before any read operation
#include <iostream.h> public: int istream::ipfx( int noskipws = 0 );
The ipfx() public member function is a prefix function that's executed before each of the formatted and unformatted read operations. If any bits are set in ios::iostate, the ipfx() public member function immediately returns 0, indicating that the prefix function failed. Failure in the prefix function causes the input operation to fail.
If the noskipws parameter is 0 or unspecified and the ios::skipws bit is on in ios::fmtflags, whitespace characters are discarded and the istream object is positioned so that the next character read is the first character after the discarded whitespace. Otherwise, no whitespace skipping takes place.
The formatted input functions that read specific types of objects (such as integers and floating-point numbers) call the ipfx() public member function with the noskipws parameter set to zero, allowing leading whitespaces to be discarded if the ios::skipws bit is on in ios::fmtflags. The unformatted input functions that read characters without interpretation call the ipfx() public member function with a the noskipws parameter set to 1 so that no whitespace characters are discarded.
If the istream object is tied to an output stream, the output stream is flushed.
If the istream object isn't in an error state in the inherited ios object when the above processing is completed, the ipfx() public member function returns a non-zero value to indicate success. Otherwise, zero is returned to indicate failure.
istream::isfx(), ios::fmtflags, ios::iostate
a suffix function that's called after any read operation
#include <iostream.h> public: void istream::isfx();
The isfx() public member function is a suffix function executed just before the end of each of the formatted and unformatted read operations.
As currently implemented, the isfx() public member function doesn't do anything.
create a istream object
#include <iostream.h> protected: istream::istream(); public: istream::istream( istream const &istrm ); istream::istream( streambuf *sb );
There are three forms of the istream constructor:
This form of the protected istream constructor is only used implicitly by the compiler when it generates a constructor for a derived class.
This function is likely to be used for the creation of an istream object that's associated with the same streambuf object as another istream object.
istream::~istream(), ios::iostate
destroy a istream object
#include <iostream.h> public: virtual istream::~istream();
The public virtual istream destructor doesn't do anything explicit. The ios destructor is called for that portion of the istream object. The call to the public virtual istream destructor is inserted implicitly by the compiler at the point where the istream object goes out of scope.
The istream object is destroyed.
associate a streambuf object with the istream object
#include <iostream.h> public: istream &istream::operator =( streambuf *sb ); istream &istream::operator =( istream const &istrm );
There are two forms of the operator =() public member function:
read characters
#include <iostream.h> public: // read characters istream &istream::operator >> ( char *buf ); istream &istream::operator >> ( signed char *buf ); istream &istream::operator >> ( unsigned char *buf ); // read a single character istream &istream::operator >> ( char &ch ); istream &istream::operator >> ( signed char &ch ); istream &istream::operator >> ( unsigned char &ch ); // read an integer istream &istream::operator >> ( signed int &num ); istream &istream::operator >> ( unsigned int &num ); istream &istream::operator >> ( signed long &num ); istream &istream::operator >> ( unsigned long &num ); istream &istream::operator >> ( signed short &num ); istream &istream::operator >> ( unsigned short &num ); // read a floating-point value istream &istream::operator >> ( float &num ); istream &istream::operator >> ( double &num ); istream &istream::operator >> ( long double &num ); // read until end-of-file istream &istream::operator >>( streambuf &sb ); // call a non-parameterized manipulator istream &istream::operator >> ( istream &(*fn)( istream & ) ); istream &istream::operator >> ( ios &(*fn)( ios & ) );
The operator >>() public member function performs a formatted read. The parameters determine what's read.
These forms of the operator >>() public member function perform a formatted read of characters from the istream object and place them in the buffer specified by the buf parameter. Characters are read until a whitespace character is found or the maximum size has been read. If a whitespace character is found, it isn't transferred to the buffer, and remains in the istream object.
If a non-zero format width has been specified, it's interpreted as the maximum number of characters that may be placed in buf. No more than format width-1 characters are read from the istream object and transferred to buf. If format width is zero, characters are transferred until a whitespace character is found.
Since these forms of the operator >>() public member function use format width, it's reset to zero after each use. It must be set before each input operation that requires a non-zero format width. |
A null character is added following the last transferred character, even if the transfer fails because of an error.
These forms of the operator >>() public member function perform a formatted read of a single character from the istream object and place it in the ch parameter.
These forms the operator >>() public member function perform a formatted read of an integral value from the istream object, and place it in the num parameter.
The number may be preceded by a + or - sign.
If ios::dec is the only bit set in the ios::basefield bits of ios::fmtflags, the number is interpreted as a decimal (base 10) integer, composed of the digits 0123456789.
If ios::oct is the only bit set in the ios::basefield bits of ios::fmtflags, the number is interpreted as an octal (base 8) integer, composed of the digits 01234567.
If ios::hex is the only bit set in the ios::basefield bits of ios::fmtflags, the number is interpreted as a hexadecimal (base 16) integer, composed of the digits 0123456789 and the letters abcdef or ABCDEF.
If no bits are set in the ios::basefield bits of ios::fmtflags, the operator looks for a prefix to determine the base of the number. If the first two characters are 0x or 0X, the number is interpreted as a hexadecimal number. If the first character is a 0 (and the second isn't an x or X), the number is interpreted as an octal integer. Otherwise, no prefix is expected, and the number is interpreted as a decimal integer.
If more than one bit is set in the ios::basefield bits of ios::fmtflags, the number is interpreted as a decimal integer.
These forms of the operator >>() public member function perform a formatted read of a floating-point value from the istream object and place it in the num parameter.
The floating-point value may be specified in any form that's acceptable to the C++ compiler.
This form of the operator >>() public member function transfers all the characters from the istream object into the sb parameter. Reading continues until end-of-file is encountered.
These forms of the operator >>() public member function are used to implement the non-parameterized manipulators for the istream class. The function specified by the fn parameter is called with the istream object as its parameter.
These forms of the operator >>() public member function return a reference to the istream object so that further extraction operations may be specified in the same statement. If no characters are transferred to buf, ios::failbit is set in the error state in the inherited ios object. If the first character read yielded end-of-file, ios::eofbit is set in the error state in the inherited ios object.
These forms of the operator >>() public member function return a reference to the istream object so that further extraction operations may be specified in the same statement. If the character read yielded end-of-file, ios::eofbit is set in the error state in the inherited ios object.
These forms of the operator >>() public member function return a reference to the istream object so that further extraction operations may be specified in the same statement. If end-of-file is encountered as the first character, ios::eofbit is set in the error state in the inherited ios object. If an overflow occurs while converting to the required integer type, the ios::failbit is set in the error state in the inherited ios object.
These forms of the operator >>() public member function return a reference to the istream object so that further extraction operations may be specified in the same statement. If end-of-file is encountered as the first character, ios::eofbit is set in the error state in the inherited ios object. If an overflow occurs while converting to the required type, the ios::failbit is set in the error state in the inherited ios object.
This form of the operator >>() public member function returns a reference to the istream object, so that further extraction operations may be specified in the same statement.
These forms of the operator >>() public member function return a reference to the istream object so that further extraction operations may be specified in the same statement.
istream::get(), istream::getline(), istream::read(), ios::fmtflags, ios::iostate
look up the next character, without extracting it
#include <iostream.h> public: int istream::peek();
The peek() public member function looks up the next character to be extracted from the istream object, without extracting it.
The peek() public member function returns the next character to be extracted from the istream object. If the istream object is positioned at end-of-file, EOF is returned.
try to put the extracted character back into the istream object
#include <iostream.h> public: istream &istream::putback( char ch );
The putback() public member function attempts to put the extracted character specified by the ch parameter back into the istream object. The ch character must be the same as the character before the current position of the istream object, usually the last character extracted from the stream. If it isn't the same character, the result of the next character extraction is undefined.
The number of characters that can be put back is defined by the istream object, but is usually at least 4. Depending on the status of the buffers used for input, it might be possible to put back more than 4 characters.
The putback() public member function returns a reference to the istream object. If the putback() public member function is unable to put back the ch parameter, ios::failbit is set in the error state in the inherited ios object.
read a number of characters
#include <iostream.h> public: istream &istream::read ( char *buf, int len ); istream &istream::read ( signed char *buf, int len ); istream &istream::read ( unsigned char *buf, int len );
The read() public member function performs an unformatted read of at most len characters from the istream object and stores them in the memory locations starting at buf. If end-of-file is encountered before len characters have been transferred, the transfer stops and ios::failbit is set in the error state in the inherited ios object.
The number of characters extracted can be determined with the gcount() member function.
The read() public member function returns a reference to the istream object. If end-of-file is encountered as the first character, ios::eofbit is set in the error state in the inherited ios object. If end-of-file is encountered before len characters are transferred, ios::failbit is set in the error state in the inherited ios object.
istream::gcount(), istream::get(), istream::getline(), ios::iostate
position the istream object
#include <iostream.h> public: istream &istream::seekg( streampos pos ); istream &istream::seekg( streamoff offset, ios::seekdir dir );
There are two forms of the seekg() public member function:
The dir parameter may be ios::beg, ios::cur or ios::end, and is interpreted in conjunction with the offset parameter as follows:
If the dir parameter has any other value, or the offset parameter doesn't have an appropriate sign, the seekg() public member function fails.
The seekg() public member function returns a reference to the istream object. If the seek operation fails, ios::failbit is set in the error state in the inherited ios object.
istream::tellg(), ios::iostate, ostream::tellp(), ostream::seekp()
synchronize the istream and the source of characters
#include <iostream.h> public: int istream::sync();
The sync() public member function synchronizes the input buffer and the istream object with whatever source of characters is being used. The sync() public member function uses the streambuf class's sync() virtual member function to carry out the synchronization. The specific behavior depends on what type of streambuf-derived object is associated with the istream object.
The sync() public member function returns __NOT_EOF on success, otherwise EOF is returned.
determine the position of the next available character
#include <iostream.h> public: streampos istream::tellg();
The tellg() public member function determines the position in the istream object of the next character available for reading. The first character in an istream object is at offset zero.
The tellg() public member function returns the position of the next character available for reading.
ostream::tellp(), ostream::seekp(), istream::seekg()
strstrea.h
strstreambase, istream
The istrstream class is used to create and read from string stream objects.
The istrstream class provides little of its own functionality. Derived from the strstreambase and istream classes, its constructors and destructor provide simplified access to the appropriate equivalents in those base classes.
Of the available I/O stream classes, creating an istrstream object is the preferred method of performing read operations from a string stream.
The following member functions are declared in the public interface:
istrstream( char * ); istrstream( signed char * ); istrstream( unsigned char * ); istrstream( char *, int ); istrstream( signed char *, int ); istrstream( unsigned char *, int ); ~istrstream();
istream, ostrstream, strstreamand strstreambase class descriptions
create a istrstream object
#include <strstrea.h> public: istrstream::istrstream( char *str ); istrstream::istrstream( signed char *str ); istrstream::istrstream( unsigned char *str ); istrstream::istrstream( char *str, int len ); istrstream::istrstream( signed char *str, int len ); istrstream::istrstream( unsigned char *str, int len );
There are six forms of the public istrstream constructor:
The public istrstream constructor creates an initialized istrstream object.
destroy a istrstream object
#include <strstrea.h> public: istrstream::~istrstream();
The public istrstream destructor doesn't do anything explicit. The call to this destructor is inserted implicitly by the compiler at the point where the istrstream object goes out of scope.
The istrstream object is destroyed.
iostream.h and iomanip.h
Manipulators are designed to be inserted into or extracted from a stream. Manipulators come in two forms, non-parameterized and parameterized. The non-parameterized manipulators are simpler, and are declared in iostream.h. The parameterized manipulators are more complex, and are declared in iomanip.h.
iomanip.h defines two macros, SMANIP_define() and SMANIP_make() to implement parameterized manipulators. The workings of these macros are disclosed in the header file, and aren't discussed here.
The following non-parameterized manipulators are declared in iostream.h:
ios &dec( ios & ); ios &hex( ios & ); ios &oct( ios & ); istream &ws( istream & ); ostream &endl( ostream & ); ostream &ends( ostream & ); ostream &flush( ostream & );
The following parameterized manipulators are declared in iomanip.h:
SMANIP_define( long ) resetiosflags( long ); SMANIP_define( int ) setbase( int ); SMANIP_define( int ) setfill( int ); SMANIP_define( long ) setiosflags( long ); SMANIP_define( int ) setprecision( int ); SMANIP_define( int ) setw( int ); SMANIP_define( int ) setwidth( int );
specify decimal formatting
#include <iostream.h> ios &dec( ios &strm );
The dec() manipulator sets the ios::basefield bits for decimal formatting in ios::fmtflags in the strm ios object.
write a new-line character, and flush
#include <iostream.h> ostream &endl( ostream &ostrm );
The endl() manipulator writes a new-line character to the stream specified by the ostrm parameter and performs a flush.
write a null character to the stream
#include <iostream.h> ostream &ends( ostream &ostrm );
The ends() manipulator writes a null character to the stream specified by the ostrm parameter.
flush the stream
#include <iostream.h> ostream &flush( ostream &ostrm );
The flush() manipulator flushes the stream specified by the ostrm parameter. The flush is performed in the same manner as the ostream::flush() member function.
specify hexadecimal formatting
#include <iostream.h> ios &hex( ios &strm );
The hex() manipulator sets the ios::basefield bits for hexadecimal formatting in ios::fmtflags in the strm ios object.
specify octal formatting
#include <iostream.h> ios &oct( ios &strm );
The oct() manipulator sets the ios::basefield bits for octal formatting in ios::fmtflags in the strm ios object.
reset formatting flags
#include <iomanip.h> SMANIP_define( long ) resetiosflags( long flags )
The resetiosflags() manipulator turns off the bits in ios::fmtflags that correspond to the bits that are on in the flags parameter. No other bits are affected.
ios::flags(), ios::fmtflags, ios::setf(), ios::unsetf()
set the base
#include <iomanip.h> SMANIP_define( int ) setbase( int base );
The setbase() manipulator sets the ios::basefield bits in ios::fmtflags to the value specified by the base parameter within the stream that the setbase() manipulator is operating upon.
set the fill character
#include <iomanip.h> SMANIP_define( int ) setfill( int fill )
The setfill() manipulator sets the fill character to the value specified by the fill parameter within the stream that the setfill() manipulator is operating upon.
set formatting flags
#include <iomanip.h> SMANIP_define( long ) setiosflags( long flags );
The setiosflags() manipulator turns on the bits in ios::fmtflags that correspond to the bits that are on in the flags parameter. No other bits are affected.
ios::flags(), ios::fmtflags, ios::setf(), ios::unsetf()
set the format precision
#include <iomanip.h> SMANIP_define( int ) setprecision( int prec );
The setprecision() manipulator sets the format precision to the value specified by the prec parameter within the stream that the setprecision() manipulator is operating upon.
set the format width
#include <iomanip.h> SMANIP_define( int ) setw( int wid );
The setw() manipulator sets the format width to the value specified by the wid parameter within the stream that the setw() manipulator is operating upon.
ios::width(), manipulator setwidth()
set the format width
#include <iomanip.h> SMANIP_define( int ) setwidth( int wid );
The setwidth() manipulator sets the format width to the value specified by the wid parameter within the stream that the setwidth() manipulator is operating upon.
This function is a WATCOM extension. |
ios::width(), manipulator setw()
extract and discard whitespace characters
#include <iostream.h> istream &ws( istream &istrm );
The ws() manipulator extracts and discards whitespace characters from the istrm parameter, leaving the stream positioned at the next non-whitespace character.
The ws() manipulator is needed particularly when the ios::skipws bit isn't set in ios::fmtflags in the istrm object. In this case, whitespace characters must be explicitly removed from the stream, since the formatted input operations won't automatically remove them.
ios::fmtflags, istream::eatwhite(), istream::ignore()
fstream.h
fstreambase, ostream
The ofstream class is used to create new files or access existing files for writing. The file can be opened and closed, and write and seek operations can be performed.
The ofstream class provides very little of its own functionality. Derived from both the fstreambase and ostream classes, its constructors, destructor and member function provide simplified access to the appropriate equivalents in those base classes.
Of the available I/O stream classes, creating an ofstream object is the preferred method of accessing a file for output operations.
The following public member functions are declared:
ofstream(); ofstream( char const *, ios::openmode = ios::out, int = filebuf::openprot ); ofstream( filedesc ); ofstream( filedesc, char *, int ); ~ofstream(); void open( char const *, ios::openmode = ios::out, int = filebuf::openprot );
fstream, fstreambase, ifstream and ostream class descriptions
create a ofstream object
#include <fstream.h> public: ofstream::ofstream(); ofstream::ofstream( const char *name, ios::openmode mode = ios::out, int prot = filebuf::openprot ); ofstream::ofstream( filedesc hdl ); ofstream::ofstream( filedesc hdl, char *buf, int len );
There are four forms of the public ofstream constructor:
If thebuf parameter is NULL or the len is less than or equal to zero, the filebuf is unbuffered, so that each read or write operation reads or writes a single character at a time.
filebuf::openprot, fstreambase::attach(), fstreambase::close(), fstreambase::fd(), fstreambase::setbuf(), ios::iostate, ios::openmode, ofstream::~ofstream(), ofstream::open()
destroy a ofstream object
#include <fstream.h> public: ofstream::~ofstream();
The public ofstream destructor doesn't do anything explicit. The call to this destructor is inserted implicitly by the compiler at the point where the ofstream object goes out of scope.
The ofstream object is destroyed.
connect the ofstream object to a file
#include <fstream.h> public: void ofstream::open( const char *name, ios::openmode mode = ios::out, int prot = filebuf::openprot );
The open() public member function connects the ofstream object to the file specified by the name parameter, using the specified mode and prot parameters.
The mode parameter is optional, and usually isn't specified unless additional bits (such as ios::binary or ios::text) are to be specified. The connection is made via the C library open() function.
If the open() fails, ios::failbit is set in the error state in the inherited ios object.
ofstream::ofstream(), ios::iostate, ios::openmode, filebuf::openprot, fstreambase::attach(), fstreambase::close(), fstreambase::fd(), fstreambase::is_open()
iostream.h
ios
iostream, ofstream, ostrstream
The ostream class supports writing characters to a class derived from the streambuf class, and provides formatted conversion of types (such as integers and floating-point numbers) into characters. The class derived from the streambuf class provides the methods for communicating with the external device (screen, disk), while the ostream class provides the conversion of the types into characters.
Generally, ostream objects won't be explicitly created by a program, since there is no mechanism at this level to open a device. The only default ostream objects in a program are cout, cerr and clog, which write to the standard output and error devices (usually the screen).
The ostream class supports two basic concepts of output: formatted and unformatted. The overloaded operator <<() member functions are called inserters, and they provide the support for formatted output. The rest of the member functions deal with unformatted output, managing the state of the ios object, and providing a friendlier interface to the associated streambuf object.
The following protected member functions are declared:
ostream();
The following public member functions are declared:
ostream( ostream const & ); ostream( streambuf * ); virtual ~ostream(); ostream &flush(); int opfx(); void osfx(); ostream &put( char ); ostream &put( signed char ); ostream &put( unsigned char ); ostream &seekp( streampos ); ostream &seekp( streamoff, ios::seekdir ); streampos tellp(); ostream &write( char const *, int ); ostream &write( signed char const *, int ); ostream &write( unsigned char const *, int );
The following public member operators are declared:
ostream &operator =( streambuf * ); ostream &operator =( ostream const & ); ostream &operator <<( char ); ostream &operator <<( signed char ); ostream &operator <<( unsigned char ); ostream &operator <<( signed short ); ostream &operator <<( unsigned short ); ostream &operator <<( signed int ); ostream &operator <<( unsigned int ); ostream &operator <<( signed long ); ostream &operator <<( unsigned long ); ostream &operator <<( float ); ostream &operator <<( double ); ostream &operator <<( long double ); ostream &operator <<( void * ); ostream &operator <<( streambuf & ); ostream &operator <<( char const * ); ostream &operator <<( signed char const * ); ostream &operator <<( unsigned char const * ); ostream &operator <<( ostream &(*)( ostream & ) ); ostream &operator <<( ios &(*)( ios & ) );
ios, iostream and istream class descriptions
flush the ostream object's buffers
#include <iostream.h> public: ostream &ostream::flush();
The flush() public member function causes the ostream object's buffers to be flushed, forcing the contents to be written to the actual device connected to the ostream object.
The flush() public member function returns a reference to the ostream object. On failure, ios::failbit is set in the error state in the inherited ios object.
perform a formatted write
#include <iostream.h> public: // write a character ostream &ostream::operator <<( char ch ); ostream &ostream::operator <<( signed char ch ); ostream &ostream::operator <<( unsigned char ch ); // write a string ostream &ostream::operator <<( char const *str ); ostream &ostream::operator <<( signed char const *str ); ostream &ostream::operator <<( unsigned char const *str ); // write an integer ostream &ostream::operator <<( signed int num ); ostream &ostream::operator <<( unsigned int num ); ostream &ostream::operator <<( signed long num ); ostream &ostream::operator <<( unsigned long num ); ostream &ostream::operator <<( signed short num ); ostream &ostream::operator <<( unsigned short num ); // write a floating-point number ostream &ostream::operator <<( float num ); ostream &ostream::operator <<( double num ); ostream &ostream::operator <<( long double num ); // write a pointer ostream &ostream::operator <<( void *ptr ); // write the contents of a stream buffer ostream &ostream::operator <<( streambuf &sb ); // implement non-parameterized manipulators ostream &ostream::operator <<( ostream &(*fn)( ostream &) ); ostream &ostream::operator <<( ios &(*fn)( ios & ) );
The operator <<() public member function does a formatted write into the ostream object. The parameters determine what's being written.
These forms of the operator <<() public member function write the ch character into the ostream object.
These forms of the operator <<() public member function perform a formatted write of the contents of the C string specified by the str parameter to the ostream object. The characters from str are transferred up to, but not including the terminating null character.
These forms of the operator <<() public member function perform a formatted write of the integral value specified by the num parameter to the ostream object. The integer value is converted to a string of characters which are written to the ostream object.
num is converted to a base representation depending on the setting of the ios::basefield bits in ios::fmtflags. If the ios::oct bit is the only bit on, the conversion is to an octal (base 8) representation. If the ios::hex bit is the only bit on, the conversion is to a hexadecimal (base 16) representation. Otherwise, the conversion is to a decimal (base 10) representation.
For decimal conversions only, a sign may be written in front of the number. If the number is negative, a - minus sign is written. If the number is positive and the ios::showpos bit is on in ios::fmtflags, a + plus sign is written. No sign is written for a value of zero.
If the ios::showbase bit is on in ios::fmtflags, and the conversion is to octal or hexadecimal, the base indicator is written next. The base indicator for a conversion to octal is a zero. The base indicator for a conversion to hexadecimal is 0x or 0X, depending on the setting of the ios::uppercase bit in ios::fmtflags.
If the value being written is zero, the conversion is to octal, and the ios::showbase bit is on, nothing further is written since a single zero is sufficient.
The value of num is then converted to characters:
The string resulting from the conversion is then written to the ostream object.
If the ios::internal bit is set in ios::fmtflags and padding is required, the padding characters are written after the sign and/or base indicator (if present) and before the digits.
These forms of the operator <<() public member function perform a formatted write of the floating-point value specified by the num parameter to the ostream object. The number is converted to either scientific (exponential) form or fixed-point form, depending on the setting of the ios::floatfield bits in ios::fmtflags:
Scientific form consists of a minus sign (for negative numbers), one digit, a decimal point, format precision-1 digits, an e or E (depending on the setting of the ios::uppercase bit), a minus sign (for negative exponents) or a plus sign (for zero or positive exponents), and two or three digits for the exponent. The digit before the decimal isn't zero, unless the number is zero. If the format precision is zero (or one), no digits are written following the decimal point.
Fixed-point form consists of a minus sign (for negative numbers), one or more digits, a decimal point, and format precision digits.
If the ios::showpoint bit isn't set in ios::fmtflags, trailing zeroes are trimmed after the decimal point (and before the exponent for scientific form), and if no digits remain after the decimal point, the decimal point is discarded as well.
If the ios::internal bit is set in ios::fmtflags and padding is required, the padding characters are written after the sign (if present) and before the digits.
This form of the operator <<() public member function performs a formatted write of the pointer value specified by the ptr parameter to the ostream object.
The ptr parameter is converted to an implementation-defined string of characters, and written to the ostream object. With the Watcom C++ implementation, the string starts with 0x or 0X (depending on the setting of the ios::uppercase bit), followed by 4 hexadecimal digits for 16-bit pointers, and 8 hexadecimal digits for 32-bit pointers. Leading zeroes are added to ensure the correct number of digits are written. For far pointers, 4 additional hexadecimal digits and a colon (:) are inserted immediately after the 0x prefix.
This form of the operator <<() public member function transfers the contents of the sb streambuf object to the ostream object. Reading from the streambuf object stops when the read fails. No padding with the fill character takes place on output to the ostream object.
These forms of the operator <<() public member function are used to implement the non-parameterized manipulators for the ostream class. The function specified by the fn parameter is called with the ostream object as its parameter.
All of the forms of the operator <<() public member function return a reference to the ostream object, so that further insertion operations may be specified in the same statement. ios::failbit is set in the error state in the inherited ios object if an error occurs.
ios::fmtflags, ios::iostate, ostream::put()
associate a stream buffer with the ostream object
#include <iostream.h> public: ostream &ostream::operator =( streambuf *sb ); ostream &ostream::operator =( const ostream &ostrm );
There are two forms of the operator =() public member function:
Both forms of the operator =() public member function return a reference to the ostream object that's the target of the assignment.
a prefix function that's executed before writing
#include <iostream.h> public: int ostream::opfx();
If opfx() public member function is a prefix function executed before each of the formatted and unformatted output operations. If any bits are set in ios::iostate, the opfx() public member function immediately returns zero, indicating that the prefix function failed. Failure in the prefix function causes the output operation to fail.
If the ostream object is tied to another ostream object, the other ostream object is flushed.
The opfx() public member function returns a non-zero value on success, otherwise zero is returned.
ios::iostate ios::tie(), ostream::osfx(), ostream::flush()
a suffix function that's executed after writing
#include <iostream.h> public: void ostream::osfx();
The osfx() public member function is a suffix function executed at the end of each of the formatted and unformatted output operations.
If the ios::unitbuf bit is set in ios::fmtflags, the flush() member function is called. If the ios::stdio bit is set in ios::fmtflags, the C library fflush function is invoked on the stdout and stderr file streams.
ios::fmtflags, ostream::osfx(), ostream::flush()
create a ostream object
#include <iostream.h> protected: ostream::ostream(); public: ostream::ostream( ostream const &ostrm ); ostream::ostream( streambuf *sb );
There are three forms of the ostream constructor:
This form of the protected ostream constructor is only used implicitly by the compiler when it generates a constructor for a derived class.
This function is likely to be used for the creation of an ostream object that's associated with the same streambuf object as another ostream object.
All three forms of the ostream constructor create an initialized ostream object:
ios::iostate, ostream::~ostream()
destroy a ostream object
#include <iostream.h> public: virtual ostream::~ostream();
The public virtual ostream destructor doesn't do anything explicit. The ios destructor is called for that portion of the ostream object. The call to the public virtual ostream destructor is inserted implicitly by the compiler at the point where the ostream object goes out of scope.
The ostream object is destroyed.
write a character to the ostream object
#include <iostream.h> public: ostream &ostream::put( char ch ); ostream &ostream::put( signed char ch ); ostream &ostream::put( unsigned char ch );
The put() public member function writes the ch character to the ostream object.
The put() public member function returns a reference to the ostream object. If an error occurs, ios::failbit is set in the error state in the inherited ios object.
ios::iostate, ostream::operator <<(), ostream::write()
position the ostream object
#include <iostream.h> public: ostream &ostream::seekp( streampos pos ); ostream &ostream::seekp( streamoff offset, ios::seekdir dir );
There are two forms of the seekp() public member function:
The pos value is an absolute position within the stream. It may be obtained via a call to the tellp member function.
The dir parameter may be ios::beg, ios::cur, or ios::end and is interpreted in conjunction with the offset parameter as follows:
If the dir parameter has any other value, or the offset parameter doesn't have an appropriate sign, the seekp() public member function fails.
Both forms of the seekp() public member function return a reference to the ostream object. If the seek operation fails, ios::failbit is set in the error state in the inherited ios object.
ios::iostate, ostream::tellp(), istream::tellg(), istream::seekg()
return the position at which the next character will be written
#include <iostream.h> public: streampos ostream::tellp();
The tellp() public member function returns the position in the ostream object at which the next character will be written. The first character in an ostream object is at offset zero.
The tellp() public member function returns the position in the ostream object at which the next character will be written.
ostream::seekp(), istream::tellg(), istream::seekg()
perform an unformatted write
#include <iostream.h> public: ostream &ostream::write( char const *buf, int len ); ostream &ostream::write( signed char const *buf, int len ); ostream &ostream::write( unsigned char const *buf, int len );
The write() public member function performs an unformatted write of the characters specified by the buf and len parameters into the ostream object.
These member functions return a reference to the ostream object. If an error occurs, ios::failbit is set in the error state in the inherited ios object.
strstrea.h
strstreambase, ostream
The ostrstream class is used to create and write to string stream objects.
The ostrstream class provides little of its own functionality. Derived from the strstreambase and ostream classes, its constructors and destructor provide simplified access to the appropriate equivalents in those base classes. The member functions provide specialized access to the string stream object.
Of the available I/O stream classes, creating an ostrstream object is the preferred method of performing write operations to a string stream.
The following member functions are declared in the public interface:
ostrstream(); ostrstream( char *, int, ios::openmode = ios::out ); ostrstream( signed char *, int, ios::openmode = ios::out ); ostrstream( unsigned char *, int, ios::openmode = ios::out ); ~ostrstream(); int pcount() const; char *str();
istrstream, ostream, ostrstream and strstreambase class descriptions
create a ostrstream object
#include <strstrea.h> public: ostrstream::ostrstream(); ostrstream::ostrstream( char *str, int len, ios::openmode mode = ios::out ); ostrstream::ostrstream( signed char *str, int len, ios::openmode mode = ios::out ); ostrstream::ostrstream( unsigned char *str, int len, ios::openmode mode = ios::out );
There are four forms of the public ostrstream constructor:
destroy a ostrstream object
#include <strstrea.h> public: ostrstream::~ostrstream();
The public ostrstream destructor doesn't do anything explicit. The call to it is inserted implicitly by the compiler at the point where the ostrstream object goes out of scope.
The ostrstream object is destroyed.
count the characters that have been written
#include <strstrea.h> public: int ostrstream::pcount() const;
The pcount() public member function computes the number of characters that have been written to the ostrstream object. This value is particularly useful if the ostrstream object doesn't contain a C string (terminated by a null character), so that the number of characters can't be determined with the C library strlen() function.
If the ostrstream object was created by appending to a C string in a static buffer, the length of the original string is included in the character count.
The pcount() public member function returns the number of characters contained in the ostrstream object.
return a pointer to the buffer used by the ostrstream object
#include <strstrea.h> public: char *ostrstream::str();
The str() public member function creates a pointer to the buffer being used by the ostrstream object. If the ostrstream object was created without dynamic allocation (static mode), the pointer is the same as the buffer pointer passed in the constructor.
For ostrstream objects using dynamic allocation, the str() public member function makes an implicit call to the strstreambuf::freeze() member function. If nothing has been written to the ostrstream object, the returned pointer is NULL.
The buffer doesn't necessarily end with a null character. If the pointer returned by the str() public member function is to be interpreted as a C string, it's the program's responsibility to ensure that the null character is present. |
The str() public member function returns a pointer to the buffer being used by the ostrstream object.
stdiobuf.h
streambuf
The stdiobuf class specializes the streambuf class, and is used to implement the standard input/output buffering required for the cin, cout, cerr and clog predefined objects.
The stdiobuf class behaves in a similar way to the filebuf class, but doesn't need to switch between the get area and put area, since no stdiobuf object can be created for both reading and writing. When the get area is empty and a read is done, the underflow() virtual member function reads more characters and fills the get area again. When the put area is full and a write is done, the overflow() virtual member function writes the characters and makes the put area empty again.
C++ programmers who wish to use the standard input/output streams without deriving new objects don't need to explicitly create or use a stdiobuf object.
The following member functions are declared in the public interface:
stdiobuf(); stdiobuf( FILE * ); ~stdiobuf(); virtual int overflow( int = EOF ); virtual int underflow(); virtual int sync();
streambuf and ios class descriptions
deal with the put area when it's full
#include <stdiobuf.h> public: virtual int stdiobuf::overflow( int ch = EOF );
The overflow() public virtual member function provides the output communication to the standard output and standard error devices to which the stdiobuf object is connected. Member functions in the streambuf class call the overflow() public virtual member function for the derived class when the put area is full.
This function performs the following steps:
The overflow() public virtual member function returns __NOT_EOF on success, otherwise EOF is returned.
stdiobuf::underflow(), streambuf::overflow()
create a stdiobuf object
#include <stdiobuf.h> public: stdiobuf::stdiobuf(); stdiobuf::stdiobuf( FILE *fptr );
There are two forms of the public stdiobuf constructor:
The public stdiobuf constructor creates a stdiobuf object.
destroy a stdiobuf object
#include <stdiobuf.h> public: stdiobuf::~stdiobuf();
The public stdiobuf destructor doesn't do anything explicit. The streambuf destructor is called for that portion of the stdiobuf object. The call to the public stdiobuf destructor is inserted implicitly by the compiler at the point where the stdiobuf object goes out of scope.
The stdiobuf object is destroyed.
synchronize the stdiobuf object and the associated device
#include <stdiobuf.h> public: virtual int stdiobuf::sync();
The sync() public virtual member function synchronizes the stdiobuf object with the associated device. If the put area contains characters, it's flushed. If the get area contains buffered characters, the sync() public virtual member function fails.
The sync() public virtual member function returns __NOT_EOF on success, otherwise, EOF is returned.
deal with the get area when it's empty
#include <stdiobuf.h> public: virtual int stdiobuf::underflow();
The underflow() public virtual member function provides the input communication from the standard input device to which the stdiobuf object is connected. Member functions in the streambuf class call the underflow() public virtual member function for the derived class when the get area is empty.
This function performs the following steps:
The underflow() public virtual member function returns the first unread character of the get area, on success, otherwise EOF is returned. Note that the get pointer isn't advanced on success.
stdiobuf::overflow(), streambuf::underflow()
streambu.h
filebuf, stdiobuf, strstreambuf
The streambuf class is responsible for maintaining the buffer used to create an efficient implementation of the stream classes. Through its pure virtual functions, it's also responsible for the actual communication with the device associated with the stream.
The streambuf class is abstract, due to the presence of pure virtual member functions. Abstract classes may not be instantiated, only inherited. Hence, streambuf objects won't be created by user programs.
Stream objects maintain a pointer to an associated streambuf object, and present the interface that the user deals with most often. Whenever a stream member function wishes to read or write characters, it uses the rdbuf() member function to access the associated streambuf object and its member functions. Through judicious use of inline functions, most reads and writes of characters access the buffer directly without even doing a function call. Whenever the buffer gets filled (writing) or exhausted (reading), these inline functions invoke the function required to rectify the situation so that the proper action can take place.
A streambuf object can be unbuffered, but most often has one buffer that can be used for both input and output operations. The buffer (called the reserve area) is divided into two areas, called the get area and the put area. For a streambuf object being used exclusively to write, the get area is empty or not present. Likewise, a streambuf object being used exclusively for reading has an empty or non-existent put area.
The use of the get area and put area differs among the various classes derived from the streambuf class.
The filebuf class allows only the get area or the put area, but not both, to be active at a time. This follows from the capability of files opened for both reading and writing to have operations of each type performed at arbitrary locations in the file:
The stdiobuf class behaves in a similar way to the filebuf class, but doesn't need to switch between the get area and put area, since no stdiobuf object can be created for both reading and writing:
The strstreambuf class differs quite markedly from the filebuf and stdiobuf classes. Since there's no actual source or destination for the characters in strstream objects, the buffer itself takes on that role:
The reserve area is marked by two pointer values:
The setb() protected member function is used to set both pointers.
Within the reserve area, the get area is marked by three pointer values:
The setg() member function is used to set all three pointer values. If any of these pointers are NULL, there's no get area.
Also within the reserve area, the put area is marked by three pointer values:
The setp() member function is used to set all three pointer values. If any of these pointers are NULL, there's no put area.
In summary, the reserve area, get area, and put area pointer functions return the following values:
From eback() to gptr() are characters buffered and read. From gptr() to egptr() are characters buffered but not yet read. From pbase() to pptr() are characters buffered and not yet written. From pptr() to epptr() is unused buffer area.
Unbuffered I/O is also possible. If unbuffered, the overflow() virtual member function is used to write single characters directly to their final destination without using the put area. Similarly, the underflow() virtual member function is used to read single characters directly from their source without using the get area.
The following member functions are declared in the protected interface:
streambuf(); streambuf( char *, int ); virtual ~streambuf(); int allocate(); char *base() const; char *ebuf() const; int blen() const; void setb( char *, char *, int ); char *eback() const; char *gptr() const; char *egptr() const; void gbump( streamoff ); void setg( char *, char *, char *); char *pbase() const; char *pptr() const; char *epptr() const; void pbump( streamoff ); void setp( char *, char *); int unbuffered( int ); int unbuffered() const; virtual int doallocate();
The following member functions are declared in the public interface:
int in_avail() const; int out_waiting() const; int snextc(); int sgetn( char *, int ); int speekc(); int sgetc(); int sgetchar(); int sbumpc(); void stossc(); int sputbackc( char ); int sputc( int ); int sputn( char const *, int ); void dbp();
virtual int do_sgetn( char *, int ); virtual int do_sputn( char const *, int ); virtual int pbackfail( int ); virtual int overflow( int = EOF ) = 0; virtual int underflow() = 0; virtual streambuf *setbuf( char *, int ); virtual streampos seekoff( streamoff, ios::seekdir, ios::openmode = ios::in|ios::out ); virtual streampos seekpos( streampos, ios::openmode = ios::in|ios::out ); virtual int sync();
filebuf, stdiobuf and strstreambuf class descriptions
manage allocation of the reserve area
#include <streambu.h> protected: int streambuf::allocate();
The allocate() protected member function works in tandem with the doallocate() protected virtual member function to manage allocation of the streambuf object reserve area.
Classes derived from the streambuf class should call the allocate() protected member function, rather than the doallocate() protected virtual member function. |
The allocate() protected member function determines whether or not the streambuf object is allowed to allocate a buffer for use as the reserve area. If a reserve area already exists or if the streambuf object unbuffering state is non-zero, the allocate() protected member function fails. Otherwise, it calls the doallocate() protected virtual member function.
The allocate() protected member function returns __NOT_EOF on success, otherwise EOF is returned.
streambuf::doallocate(), streambuf::underflow(), streambuf::overflow()
return a pointer to the start of the reserve area
#include <streambu.h> protected: char *streambuf::base() const;
The base() protected member function returns a pointer to the start of the reserve area that the streambuf object is using.
The base() protected member function returns a pointer to the start of the reserve area that the streambuf object is using. If the streambuf object currently doesn't have a reserve area, NULL is returned.
streambuf::blen(), streambuf::ebuf(), streambuf::setb()
return the length of the reserve area
#include <streambu.h> protected: int streambuf::blen() const;
The blen() protected member function reports the length of the reserve area that the streambuf object is using.
The blen() protected member function returns the length of the reserve area that the streambuf object is using. If the streambuf object currently doesn't have a reserve area, zero is returned.
streambuf::base(), streambuf::ebuf(), streambuf::setb()
dump information about the stream buffer
#include <streambu.h> public: void streambuf::dbp();
The dbp() public member function dumps information about the streambuf object directly to stdout, and is used for debugging classes derived from the streambuf class.
The following is an example of what the dbp() public member function dumps:
STREAMBUF Debug Info: this = 00030679, unbuffered = 0, delete_reserve = 1 base = 00070010, ebuf = 00070094 eback = 00000000, gptr = 00000000, egptr = 00000000 pbase = 00070010, pptr = 00070010, epptr = 00070094
transfer characters from the get area
#include <streambu.h> public: virtual int do_sgetn( char *buf, int len );
The do_sgetn() public virtual member function works in tandem with the sgetn member function to transfer len characters from the get area into buf.
Classes derived from the streambuf class should call the sgetn member function, rather than the do_sgetn() public virtual member function. |
Classes derived from the streambuf class that implement the do_sgetn() public virtual member function should support copying up to len characters from the source through the get area and into buf.
The default do_sgetn() public virtual member function provided with the streambuf class calls the underflow() virtual member function to fetch more characters, and then copies the characters from the get area into buf.
The do_sgetn() public virtual member function returns the number of characters successfully transferred.
transfer characters from the put area
#include <streambu.h> public: virtual int do_sputn( char const *buf, int len );
The do_sputn() public virtual member function works in tandem with the sputn member function to transfer len characters from buf to the end of the put area and advances the put pointer.
Classes derived from the streambuf class should call the sputn member function, rather than the do_sputn() public virtual member function. |
Classes derived from the streambuf class that implement the do_sputn() public virtual member function should support copying up to len characters from buf through the put area and out to the destination device.
The default do_sputn() public virtual member function provided with the streambuf class calls the overflow() virtual member function to flush the put area, and then copies the rest of the characters from buf into the put area.
The do_sputn() public virtual member function returns the number of characters successfully written. If an error occurs, this number might be less than len.
manage allocation of the reserve area
#include <streambu.h> protected: virtual int streambuf::doallocate();
The doallocate() protected virtual member function manages allocation of the streambuf object's reserve area in tandem with the allocate() protected member function.
Classes derived from the streambuf class should call the
allocate() protected member function rather than the
doallocate() protected virtual member function.
The doallocate() protected virtual member function does the actual memory allocation, and can be defined for each class derived from the streambuf class. |
Classes derived from the streambuf class should implement the doallocate() protected virtual member function such that it does the following:
The default doallocate() protected virtual member function provided with the streambuf class attempts to allocate a buffer area with the operator new() intrinsic function. It then calls the setb() protected member function to set up the pointers to the reserve area.
The doallocate() protected virtual member function returns __NOT_EOF on success, otherwise EOF is returned.
return a pointer to the start of the get area
#include <streambu.h> protected: char *streambuf::eback() const;
The eback() protected member function returns a pointer to the start of the get area within the reserve area used by the streambuf object.
The eback() protected member function returns a pointer to the start of the get area. If the streambuf object currently doesn't have a get area, NULL is returned.
streambuf::egptr(), streambuf::gptr(), streambuf::setg()
return a pointer to the end of the reserve area
#include <streambu.h> protected: char *streambuf::ebuf() const;
The ebuf() protected member function returns a pointer to the end of the reserve area that the streambuf object is using. The character pointed at is actually the first character past the end of the reserve area.
The ebuf() protected member function returns a pointer to the end of the reserve area. If the streambuf object currently doesn't have a reserve area, NULL is returned.
streambuf::base(), streambuf::blen(), streambuf::setb()
return a pointer to the end of the get area
#include <streambu.h> protected: char *streambuf::egptr() const;
The egptr() protected member function returns a pointer to the end of the get area within the reserve area used by the streambuf object. The character pointed at is actually the first character past the end of the get area.
The egptr() protected member function returns a pointer to the end of the get area. If the streambuf object currently doesn't have a get area, NULL is returned.
streambuf::eback(), streambuf::gptr(), streambuf::setg()
return a pointer to the end of the put area
#include <streambu.h> protected: char *streambuf::epptr() const;
The epptr() protected member function returns a pointer to the end of the put area within the reserve area used by the streambuf object. The character pointed at is actually the first character past the end of the put area.
The epptr() protected member function returns a pointer to the end of the put area. If the streambuf object currently doesn't have a put area, NULL is returned.
streambuf::pbase(), streambuf::pptr(), streambuf::setp()
increment the get pointer
#include <streambu.h> protected: void streambuf::gbump( streamoff offset );
The gbump() protected member function increments the get pointer by the specified offset, without regard for the boundaries of the get area. The offset parameter may be positive or negative.
The gbump() protected member function returns nothing.
streambuf::gptr(), streambuf::pbump(), streambuf::sbumpc(), streambuf::sputbackc()
return a pointer to the next available character in the get area
#include <streambu.h> protected: char *streambuf::gptr() const;
The gptr() protected member function returns a pointer to the next available character in the get area within the reserve area used by the streambuf object. This pointer is called the get pointer.
If the get pointer points beyond the end of the get area, all characters in the get area have been read by the program, and a subsequent read causes the underflow() virtual member function to be called to fetch more characters from the source to which the streambuf object is attached.
The gptr() protected member function returns a pointer to the next available character in the get area. If the streambuf object currently doesn't have a get area, NULL is returned.
streambuf::eback(), streambuf::egptr(), streambuf::setg()
count the buffered characters not yet read by the program
#include <streambu.h> public: int streambuf::in_avail() const;
The in_avail() public member function computes the number of input characters buffered in the get area that haven't yet been read by the program. These characters can be read with a guarantee that no errors will occur.
The in_avail() public member function returns the number of buffered input characters.
streambuf::egptr(), streambuf::gptr()
count the buffered characters not yet written to the device
#include <streambu.h> public: int streambuf::out_waiting() const;
The out_waiting() public member function computes the number of characters that have been buffered in the put area and not yet been written to the output device.
The out_waiting() public member function returns the number of buffered output characters.
streambuf::pbase(), streambuf::pptr()
flush the put area when it's full
#include <streambu.h> public: virtual int streambuf::overflow( int ch = EOF ) = 0;
The overflow() public virtual member function is used to flush the put area when it's full.
Classes derived from the streambuf class should implement the overflow() public virtual member function so that it performs the following:
There's no default streambuf class implementation of the overflow() public virtual member function. The overflow() public virtual member function must be defined for all classes derived from the streambuf class.
The overflow() public virtual member function returns __NOT_EOF on success, otherwise EOF is returned.
filebuf::overflow(), stdiobuf::overflow(), strstreambuf::overflow()
handle a failed attempt to put a character back
#include <streambu.h> public: virtual int streambuf::pbackfail( int ch );
The pbackfail() public virtual member function is called by the sputbackc() member function when the get pointer is at the beginning of the get area, and so there's no place to put the ch parameter.
Classes derived from the streambuf class should implement the pbackfail() public virtual member function such that it attempts to put ch back into the source of the stream.
The default streambuf class implementation of the pbackfail() public virtual member function is to return EOF.
If the pbackfail() public virtual member function succeeds, it returns ch. Otherwise, it returns EOF.
return a pointer to the start of the put area
#include <streambu.h> protected: char *streambuf::pbase() const;
The pbase() protected member function returns a pointer to the start of the put area within the reserve area used by the streambuf object.
The pbase() protected member function returns a pointer to the start of the put area. If the streambuf object currently doesn't have a put area, NULL is returned.
streambuf::epptr(), streambuf::pptr(), streambuf::setp()
increment the put pointer
#include <streambu.h> protected: void streambuf::pbump( streamoff offset );
The pbump() protected member function increments the put pointer by the specified offset, without regard for the boundaries of the put area. The offset parameter may be positive or negative.
The pbump() protected member function returns nothing.
streambuf::gbump(), streambuf::pbase(), streambuf::pptr()
return a pointer to the next available space in the put area
#include <streambu.h> protected: char *streambuf::pptr() const;
The pptr() protected member function returns a pointer to the next available space in the put area within the reserve area used by the streambuf object. This pointer is called the put pointer.
If the put pointer points beyond the end of the put area, the put area is full, and a subsequent write causes the overflow() virtual member function to be called to empty the put area to the device to which the streambuf object is attached.
The pptr() protected member function returns a pointer to the next available space in the put area. If the streambuf object currently doesn't have a put area, NULL is returned.
streambuf::epptr(), streambuf::pbase(), streambuf::setp()
extract the next available character from the get area
#include <streambu.h> public: int streambuf::sbumpc();
The sbumpc() public member function extracts the next available character from the get area, and advances the get pointer. If no character is available, it calls the underflow() virtual member function to fetch more characters from the source into the get area.
Due to the sbumpc() member functions's awkward name, the sgetchar() member function was added to take its place in the WATCOM implementation. |
The sbumpc() public member function returns the next available character in the get area. If no character is available, EOF is returned.
streambuf::gbump(), streambuf::sgetc(), streambuf::sgetchar(), streambuf::sgetn(), streambuf::snextc(), streambuf::sputbackc()
move to a relative position in the stream buffer
#include <streambu.h> public: virtual streampos streambuf::seekoff( streamoff offset, ios::seekdir dir, ios::openmode mode );
The seekoff() public virtual member function is used for positioning to a relative location within the streambuf object, and hence within the device that's connected to the streambuf object. The offset and dir parameters specify the relative change in position. The mode parameter controls whether the get pointer and/or the put pointer are repositioned.
Classes derived from the streambuf class should implement the seekoff() virtual member function so that it uses its parameters in the way described below.
The mode parameter may be ios::in, ios::out, or ios::in|ios::out, and should be interpreted as follows, provided the interpretation is meaningful:
If mode has any other value, the seekoff() public virtual member function fails.
The dir parameter may be ios::beg, ios::cur, or ios::end, and is interpreted in conjunction with the offset parameter as follows:
If the dir parameter has any other value, or the offset parameter doesn't have an appropriate sign, the seekoff() public virtual member function fails.
The default implementation of the seekoff() public virtual member function provided by the streambuf class returns EOF.
The seekoff() public virtual member function returns the new position in the stream on success, otherwise EOF is returned.
move to an absolute position in the stream buffer
#include <streambu.h> public: virtual streampos streambuf::seekpos( streampos pos, ios::openmode mode = ios::in|ios::out );
The seekpos() public virtual member function is used for positioning to an absolute location within the streambuf object, and hence within the device that's connected to the streambuf object. The pos parameter specifies the absolute position. The mode parameter controls whether the get pointer and/or the put pointer are repositioned.
Classes derived from the streambuf class should implement the seekpos() public virtual member function so that it uses its parameters in the following way.
The mode parameter may be ios::in, ios::out, or ios::in|ios::out, and should be interpreted as follows, provided the interpretation is meaningful:
If mode has any other value, the seekpos() public virtual member function fails.
In general the seekpos() public virtual member function is equivalent to calling the seekoff() virtual member function with the offset set to pos, the direction set to ios::beg, and the mode set to mode.
The default implementation of the seekpos() public virtual member function provided by the streambuf class calls the seekoff() virtual member function with the offset set to pos, the direction set to ios::beg, and the mode set to mode.
The seekpos() public virtual member function returns the new position in the stream on success, otherwise EOF is returned.
set the pointers to the reserve area
#include <streambu.h> protected: void streambuf::setb( char *base, char *ebuf, int autodel );
The setb() protected member function is used to set the pointers to the reserve area that the streambuf object is using.
The base parameter is a pointer to the start of the reserve area, and corresponds to the value that the base() member function returns.
The ebuf parameter is a pointer to the end of the reserve area, and corresponds to the value that the ebuf() member function returns.
The autodel parameter indicates whether or not the streambuf object can free the reserve area when the streambuf object is destroyed or when a new reserve area is set up in a subsequent call to the setb() protected member function. If the autodel parameter is non-zero, the streambuf object can delete the reserve area, using the operator delete() intrinsic function. A zero value indicates that the buffer will be deleted elsewhere.
If either of the base or ebuf parameters is NULL, or if ebuf <= base, the streambuf object doesn't have a buffer, and input/output operations are unbuffered, unless another buffer is set up.
The setb() protected member function is used to set the reserve area pointers, while the setbuf() protected member function is used to offer a buffer to the streambuf object. |
streambuf::base(), streambuf::blen(), streambuf::ebuf(), streambuf::setbuf()
offer a buffer to the streambuf object
#include <streambu.h> public: virtual streambuf *streambuf::setbuf( char *buf, int len );
The setbuf() public virtual member function is used to offer a buffer specified by the buf and len parameters to the streambuf object for use as its reserve area.
The setbuf() public virtual member function is used to offer a buffer, while the setb() protected member function is used to set the reserve area pointers once a buffer has been accepted. |
Classes derived from the streambuf class may implement the setbuf() public virtual member function if the default behavior isn't suitable.
Derived classes that provide their own implementations of the setbuf() public virtual member function may accept or reject the offered buffer. Often, if a buffer is already allocated, the offered buffer is rejected, as it might be difficult to transfer the information from the current buffer.
The default setbuf() public virtual member function provided by the streambuf class rejects the buffer if one's already present.
If no buffer is present and either buf is NULL or len is zero, the offer is accepted and the streambuf object is unbuffered.
Otherwise, no buffer is present and one is specified. If len is less than five characters the buffer is too small and it's rejected. Otherwise, the buffer is accepted.
The setbuf() public virtual member function returns the address of the streambuf object if the offered buffer is accepted, otherwise NULL is returned.
set the three get area pointers
#include <streambu.h> protected: void streambuf::setg( char *eback, char *gptr, char *egptr );
The setg() protected member function is used to set the three get area pointers:
If any of the three parameters are NULL, there's no get area.
streambuf::eback(), streambuf::egptr(), streambuf::gptr()
set the three put area pointers
#include <streambu.h> protected: void streambuf::setp( char *pbase, char *epptr );
The setp() protected member function is used to set the three put area pointers:
If either parameter is NULL, there's no put area.
streambuf::epptr(), streambuf::pbase(), streambuf::pptr()
return the next available character in the get area
#include <streambu.h> public: int streambuf::sgetc();
The sgetc() public member function returns the next available character in the get area. The get pointer isn't advanced. If the get area is empty, the underflow() virtual member function is called to fetch more characters from the source into the get area.
Due to the sgetc member function's confusing name (the C library getc function does advance the pointer), the speekc member function was added to take its place in the WATCOM implementation. |
The sgetc() public member function returns the next available character in the get area. If no character is available, EOF is returned.
streambuf::sbumpc(), streambuf::sgetchar(), streambuf::sgetn(), streambuf::snextc(), streambuf::speekc()
get the next available character in the get area
#include <streambu.h> public: int streambuf::sgetchar();
The sgetchar() public member function extracts the next available character from the get area, and advances the get pointer. If no character is available, it calls the underflow() virtual member function to fetch more characters from the source into the get area.
Due to the sbumpc() member functions's awkward name, the sgetchar() member function was added to take its place in the WATCOM implementation. |
The sgetchar() public member function returns the next available character in the get area. If no character is available, EOF is returned.
streambuf::gbump(), streambuf::sgetc(), streambuf::sgetchar(), streambuf::sgetn(), streambuf::snextc(), streambuf::speekc(), streambuf::sputbackc()
get a number of characters from the get area
#include <streambu.h> public: int streambuf::sgetn( char *buf, int len );
The sgetn() public member function transfers up to len characters from the get area into buf. If there aren't enough characters in the get area, the do_sgetn() virtual member function is called to fetch more.
Classes derived from the streambuf class should call the sgetn() public member function, rather than the do_sgetn() virtual member function. |
The sgetn() public member function returns the number of characters transferred from the get area into buf.
streambuf::do_sgetn(), streambuf::sbumpc(), streambuf::sgetc(), streambuf::sgetchar(), streambuf::speekc()
advance the get pointer, and return the character
#include <streambu.h> public: int streambuf::snextc();
The snextc() public member function advances the get pointer, and then returns the character following the get pointer. The get pointer is left pointing at the returned character.
If the get pointer can't be advanced, the underflow() virtual member function is called to fetch more characters from the source into the get area.
The snextc() public member function advances the get pointer and returns the next available character in the get area. If there's no next available character, EOF is returned.
streambuf::sbumpc(), streambuf::sgetc(), streambuf::sgetchar(), streambuf::sgetn(), streambuf::speekc()
return the next character, without advancing the get pointer
#include <streambu.h> public: int streambuf::speekc();
The speekc() public member function returns the next available character in the get area. The get pointer isn't advanced. If the get area is empty, the underflow() virtual member function is called to fetch more characters from the source into the get area.
Due to the sgetc member function's confusing name (the C library getc function does advance the pointer), the speekc member function was added to take its place in the WATCOM implementation. |
The speekc() public member function returns the next available character in the get area. If no character is available, EOF is returned.
streambuf::sbumpc(), streambuf::sgetc(), streambuf::sgetchar(), streambuf::sgetn(), streambuf::snextc()
put a character back into the get area
#include <streambu.h> public: int streambuf::sputbackc( char ch );
The sputbackc() public member function is used to put a character back into the get area. The ch character specified must be the same as the character before the get pointer, otherwise the behavior is undefined. The get pointer is backed up by one position. At least four characters may be put back without any intervening reads.
The sputbackc() public member function returns ch on success, otherwise EOF is returned.
streambuf::gbump(), streambuf::sbumpc(), streambuf::sgetchar()
add a character to the end of the put area, advancing the put pointer
#include <streambu.h> public: int streambuf::sputc( int ch );
The sputc() public member function adds the character ch to the end of the put area and advances the put pointer. If the put area is full before the character is added, the overflow() virtual member function is called to empty the put area and write the character.
The sputc() public member function returns ch on success, otherwise EOF is returned.
streambuf::sgetc(), streambuf::sputn()
add a number of characters to the put area
#include <streambu.h> public: int streambuf::sputn( char const *buf, int len );
The sputn() public member function transfers up to len characters from buf to the end of the put area and advance the put pointer. If the put area is full or becomes full, and more characters are to be written, the do_sputn() virtual member function is called to empty the put area and finish writing the characters.
Classes derived from the streambuf class should call the sputn() public member function, rather than the do_sputn() virtual member function. |
The sputn() public member function returns the number of characters successfully written. If an error occurs, this number might be less than len.
streambuf::do_sputn(), streambuf::sputc()
advance the get pointer without returning a character
#include <streambu.h> public: void streambuf::stossc();
The stossc() public member function advances the get pointer by one without returning a character. If the get area is empty, the underflow() virtual member function is called to fetch more characters, and then the get pointer is advanced.
streambuf::gbump(), streambuf::sbumpc(), streambuf::sgetchar(), streambuf::snextc()
create a streambuf object
#include <streambu.h> protected: streambuf::streambuf(); streambuf::streambuf( char *buf, int len );
There are two forms of the protected streambuf constructor:
streambuf::~streambuf(), streambuf::setbuf()
destroy a streambuf object
#include <streambu.h> protected: virtual streambuf::~streambuf();
The streambuf object is destroyed. If the buffer was allocated by the streambuf object, it's freed. Otherwise, the buffer isn't freed, and must be freed by the user of the streambuf object. The call to the protected streambuf destructor is inserted implicitly by the compiler at the point where the streambuf object goes out of scope.
The streambuf object is destroyed.
synchronize the stream buffer and the associated device
#include <streambu.h> public: virtual int streambuf::sync();
The sync() public virtual member function is used to synchronize the streambuf object's get area and put area with the associated device.
Classes derived from the streambuf class should implement the sync() public virtual member function such that it attempts to perform the following:
The default implementation of the sync() public virtual member function provided by the streambuf class takes no action. It succeeds if the get area and the put area are empty, otherwise it fails.
The sync() public virtual member function returns __NOT_EOF on success, otherwise it returns EOF.
query and/or set the unbuffering state of the object
#include <streambu.h> protected: int ios::unbuffered() const; int ios::unbuffered( int unbuf );
The unbuffered() protected member function is used to query and/or set the unbuffering state of the streambuf object. A non-zero unbuffered state indicates that the streambuf object is unbuffered. An unbuffered state of zero indicates that the streambuf object is buffered.
The first form of the unbuffered() protected member function is used to query the current unbuffering state.
The second form of the function is used to set the unbuffering state to unbuf.
The unbuffering state only affects the allocate() protected member function, which does nothing if the unbuffering state is non-zero. Setting the unbuffering state to a non-zero value doesn't mean that future I/O operations will be unbuffered. |
To determine if current I/O operations are unbuffered, use the base() protected member function. A return value of NULL from this function indicates that unbuffered I/O operations will be used.
The unbuffered() protected member function returns the previous unbuffered state.
streambuf::allocate(), streambuf::pbase(), streambuf::setbuf()
fill the get area when it's empty
#include <streambu.h> public: virtual int streambuf::underflow() = 0;
The underflow() public virtual member function is used to fill the get area when it's empty.
Classes derived from the streambuf class should implement the underflow() public virtual member function so that it performs the following:
There's no default streambuf class implementation of the underflow() public virtual member function. The underflow() public virtual member function must be defined for all classes derived from the streambuf class.
The underflow() public virtual member function returns the first character read into the get area, or EOF if no characters could be read.
filebuf::underflow(), stdiobuf::underflow(), strstreambuf::underflow()
strstrea.h
strstreambase, iostream
The strstream class is used to create and write to string stream objects.
The strstream class provides little of its own functionality. Derived from the strstreambase and iostream classes, its constructors and destructor provide simplified access to the appropriate equivalents in those base classes. The member functions provide specialized access to the string stream object.
Of the available I/O stream classes, creating a strstream object is the preferred method of performing read and write operations on a string stream.
The following member functions are declared in the public interface:
strstream(); strstream( char *, int, ios::openmode = ios::in|ios::out ); strstream( signed char *, int, ios::openmode = ios::in|ios::out ); strstream( unsigned char *, int, ios::openmode = ios::in|ios::out ); ~strstream(); char *str();
istrstream, ostrstream and strstreambase class descriptions
return a pointer to the buffer used by the strstream object
#include <strstrea.h> public: char *strstream::str();
The str() public member function creates a pointer to the buffer being used by the strstream object. If the strstream object was created without dynamic allocation (static mode), the pointer is the same as the buffer pointer passed in the constructor.
For strstream objects using dynamic allocation, the str() public member function makes an implicit call to the strstreambuf::freeze() member function. If nothing has been written to the strstream object, the returned pointer will be NULL.
The buffer doesn't necessarily end with a null character. If the pointer returned by the str() public member function is to be interpreted as a C string, it's the program's responsibility to ensure that the null character is present. |
The str() public member function returns a pointer to the buffer being used by the strstream object.
strstreambuf::str(), strstreambuf::freeze()
create a strstream object, with dynamic allocation
#include <strstrea.h> public: strstream::strstream(); strstream::strstream( char *str, int len, ios::openmode mode ); strstream::strstream( signed char *str, int len, ios::openmode mode ); strstream::strstream( unsigned char *str, int len, ios::openmode mode );
There are four forms of the public strstream constructor:
The get pointer and put pointer aren't necessarily pointing at the same location, so moving one pointer (for example, by doing a write) doesn't affect the location of the other pointer. |
destroy a strstream object
#include <strstrea.h> public: strstream::~strstream();
The public strstream destructor doesn't do anything explicit. The call to it is inserted implicitly by the compiler at the point where the strstream object goes out of scope.
The strstream object is destroyed.
strstrea.h
ios
istrstream, ostrstream, strstream
The strstreambase class is a base class that provides common functionality for the three string stream-based classes, istrstream, ostrstream and strstream. The strstreambase class is derived from the ios class, which provides the stream state information. The strstreambase class provides constructors for string stream objects and one member function.
The following member functions are declared in the protected interface:
strstreambase(); strstreambase( char *, int, char * = 0 ); ~strstreambase();
The following member function is declared in the public interface:
strstreambuf *rdbuf() const;
istrstream, ostrstream, strstream and strstreambuf class descriptions
return a pointer to the strstreambuf object
#include <strstrea.h> public: strstreambuf *strstreambase::rdbuf() const;
The rdbuf() public member function creates a pointer to the strstreambuf associated with the strstreambase object. Since the strstreambuf object is embedded within the strstreambase object, this function never returns NULL.
The rdbuf() public member function returns a pointer to the strstreambuf associated with the strstreambase object.
create a strstreambase object, with dynamic allocation
#include <strstrea.h> protected: strstreambase::strstreambase(); strstreambase::strstreambase( char *str, int len, char *pstart );
There are two forms of the protected strstreambase constructor:
This form of the protected strstreambase constructor is only used implicitly by the compiler when it generates a constructor for a derived class.
This form of the protected strstreambase constructor is unlikely to be explicitly used, except in the member initializer list for the constructor of a derived class.
The str, len and pstart parameters are interpreted as follows:
The protected strstreambase constructor creates an initialized strstreambase object.
strstreambase::~strstreambase()
destroy a strstreambase object
#include <strstrea.h> protected: strstreambase::~strstreambase();
The protected strstreambase destructor doesn't do anything explicit. The call to it is inserted implicitly by the compiler at the point where the strstreambase object goes out of scope.
The strstreambase object is destroyed.
strstreambase::strstreambase()
strstrea.h
streambuf
The strstreambuf class is derived from the streambuf class, and provides additional functionality required to write characters to and read characters from a string buffer. Read and write operations can occur at different positions in the string buffer, since the get pointer and put pointer aren't necessarily connected. Seek operations are also supported.
The reserve area used by the strstreambuf object may be either fixed in size or dynamic. Generally, input strings are of a fixed size, while output streams are dynamic, since the final size might not be predictable. For dynamic buffers, the strstreambuf object automatically grows the buffer when necessary.
The strstreambuf class differs quite markedly from the filebuf and stdiobuf classes. Since there's no actual source or destination for the characters in strstream objects, the buffer itself takes on that role:
C++ programmers who wish to use string streams without deriving new objects will probably never explicitly create or use a strstreambuf object.
The following member function is declared in the protected interface:
virtual int doallocate();
The following member functions are declared in the public interface:
strstreambuf(); strstreambuf( int ); strstreambuf( void *(*)( long ), void (*)( void * ) ); strstreambuf( char *, int, char * = 0 ); ~strstreambuf(); int alloc_size_increment( int ); void freeze( int = 1 ); char *str(); virtual int overflow( int = EOF ); virtual int underflow(); virtual streambuf *setbuf( char *, int ); virtual streampos seekoff( streamoff, ios::seekdir, ios::openmode ); virtual int sync();
streambuf and strstreambase class descriptions
modify the allocation size
#include <strstrea.h> public: int strstreambuf::alloc_size_increment( int increment );
The alloc_size_increment() public member function modifies the allocation size used when the buffer is first allocated or reallocated by dynamic allocation. The increment parameter is added to the previous allocation size for future use.
This function is a WATCOM extension. |
The alloc_size_increment() public member function returns the previous value of the allocation size.
strstreambuf::doallocate(), strstreambuf::setbuf()
deal with the put area when it's full
#include <strstrea.h> protected: virtual int strstreambuf::doallocate();
The doallocate() protected virtual member function is called by the allocate() member function when it's determined that the put area is full and needs to be extended.
The doallocate() protected virtual member function performs the following steps:
The doallocate() protected virtual member function returns __NOT_EOF on success, otherwise it returns EOF.
strstreambuf::alloc_size_increment(), strstreambuf::setbuf()
enable and disable automatic deletion of the reserve area
#include <strstrea.h> public: void strstreambuf::freeze( int frozen = 1 );
The freeze() public member function enables and disables automatic deletion of the reserve area. If the freeze() public member function is called with no parameter or a non-zero parameter, the strstreambuf object is frozen. If the freeze() public member function is called with a zero parameter, the strstreambuf object is unfrozen.
A frozen strstreambuf object doesn't free the reserve area in the destructor. If the strstreambuf object is destroyed while it's frozen, it's the program's responsibility to free the reserve area.
If characters are written to the strstreambuf object while it's frozen, the effect is undefined, since the reserve area might be reallocated and therefore might move. However, if the strstreambuf object is frozen and then unfrozen, characters may be written to it.
The freeze() public member function returns the previous frozen state.
strstreambuf::str(), strstreambuf::~strstreambuf()
attempt to grow the put area
#include <strstrea.h> public: virtual int strstreambuf::overflow( int ch = EOF );
The overflow() public virtual member function provides the output communication between the streambuf member functions and the strstreambuf object. Member functions in the streambuf class call the overflow() public virtual member function when the put area is full. The overflow() public virtual member function attempts to grow the put area so that writing may continue.
The overflow() public virtual member function performs the following steps:
The overflow() public virtual member function returns __NOT_EOF when it successfully extends the put area, otherwise it returns EOF.
streambuf::overflow(), strstreambuf::underflow()
position the get pointer and/or put pointer
#include <strstrea.h> public: virtual streampos strstreambuf::seekoff( streamoff offset, ios::seekdir dir, ios::openmode mode );
The seekoff() public virtual member function positions the get pointer and/or put pointer at the specified position in the reserve area:
The seekoff() public virtual member function seeks offset bytes from the position specified by the dir parameter.
The mode parameter may be ios::in, ios::out, or ios::in|ios::out, and should be interpreted as follows, provided the interpretation is meaningful:
If mode has any other value, the seekoff() public virtual member function fails. ios::in|ios::out isn't valid if the dir parameter is ios::cur.
The dir parameter may be ios::beg, ios::cur, or ios::end and is interpreted in conjunction with the offset parameter as follows:
If the dir parameter has any other value, or the offset parameter doesn't have an appropriate sign, the seekoff() public virtual member function fails.
The seekoff() public virtual member function returns the new position in the file on success, otherwise it returns EOF. If both of ios::in|ios::out are specified, and the dir parameter is ios::cur the returned position refers to the put pointer.
control the size of dynamic allocations
#include <strstrea.h> public: virtual streambuf *strstreambuf::setbuf( char *, int size );
The setbuf() public virtual member function is used to control the size of the allocations when the strstreambuf object is using dynamic allocation. The first parameter is ignored. The next time an allocation is required, at least the number of characters specified in the size parameter is allocated. If the specified size isn't sufficient, the allocation reverts to its default behavior, which is to extend the buffer by DEFAULT_MAINBUF_SIZE, which is 512 characters.
If a program is going to write a large number of characters to the strstreambuf object, it should call the setbuf() public virtual member function to indicate the size of the next allocation, to prevent multiple allocations as the buffer gets larger.
The setbuf() public virtual member function returns a pointer to the strstreambuf object.
strstreambuf::alloc_size_increment(), strstreambuf::doallocate()
freeze the object, and return a pointer to the reserve area
#include <strstrea.h> public: char *strstreambuf::str();
The str() public member function freezes the strstreambuf object and returns a pointer to the reserve area. This pointer remains valid after the strstreambuf object is destroyed, provided the strstreambuf object remains frozen, since the destructor doesn't free the reserve area if it's frozen.
The returned pointer might be NULL if the strstreambuf object is using dynamic allocation but hasn't yet had anything written to it.
If the strstreambuf object isn't using dynamic allocation, the pointer returned by the str() public member function is the same buffer pointer provided to the constructor. For a strstreambuf object using dynamic allocation, the pointer points to a dynamically allocated area.
The reserve area doesn't necessarily end with a null character. If the pointer returned by the str() public member function is to be interpreted as a C string, it's the program's responsibility to ensure that the null character is present. |
The str() public member function returns a pointer to the reserve area and freezes the strstreambuf object.
create a strstreambuf object
#include <strstrea.h> public: strstreambuf::strstreambuf(); strstreambuf::strstreambuf( int alloc_size ); strstreambuf::strstreambuf( void * (*alloc_fn)( long ), void (*free_fn)( void * ) ); strstreambuf::strstreambuf( char *str, int len, char *pstart = NULL ); strstreambuf::strstreambuf( signed char *str, int len, signed char *pstart = NULL ); strstreambuf::strstreambuf( unsigned char *str, int len, unsigned char *pstart = NULL );
There are six forms of the public strstreambuf constructor:
No reserve area is allocated to start. Whenever characters are written to extend the strstreambuf object, the reserve area is reallocated and copied as required. The size of allocation is determined by the strstreambuf object, unless the setbuf() or alloc_size_increment member functions are called to change the allocation size. The default allocation size is determined by the constant DEFAULT_MAINBUF_SIZE, which is 512.
No buffer is allocated to start. Whenever characters are written to extend the strstreambuf object, the reserve area is reallocated and copied as required. The size of the first allocation is determined by the alloc_size parameter, unless changed by a call to the setbuf() or alloc_size_increment member functions.
The alloc_size parameter is the starting reserve area size. When the reserve area is reallocated, the strstreambuf object uses DEFAULT_MAINBUF_SIZE to increase the reserve area size, unless the setbuf() or alloc_size_increment member functions have been called to specify a new allocation size. |
No buffer is allocated to start. Whenever characters are written to extend the strstreambuf object, the reserve area is reallocated and copied as required, using the specified alloc_fn and free_fn functions. The size of allocation is determined by the class unless the setbuf() or alloc_size_increment() member functions are called to change the allocation size. The default allocation size is determined by the constant DEFAULT_MAINBUF_SIZE, which is 512.
When a new reserve area is allocated, the function specified by the alloc_fn parameter is called with a long integer value indicating the number of bytes to allocate. If alloc_fn is NULL, the operator new() intrinsic function is used. Likewise, when the reserve area is freed, the function specified by the free_fn parameter is called with the pointer returned by the alloc_fn function as the parameter. If free_fn is NULL, the operator delete() intrinsic function is used.
The str, len and pstart parameters are interpreted as follows:
If the get area is exhausted and characters have been written to the put area, the get area is extended to include the put area.
Note that the get pointer and put pointer don't necessarily point at the same position in the reserve area, so a read followed by a write doesn't imply that the write stores following the last character read. The get pointer is positioned following the last read operation, and the put pointer is positioned following the last write operation, unless the seekoff() member function has been used to reposition the pointer(s).
The public strstreambuf constructor creates a strstreambuf object.
strstreambuf::alloc_size_increment(), strstreambuf::doallocate(), strstreambuf::setbuf(), strstreambuf::~strstreambuf()
destroy a strstreambuf object
#include <strstrea.h> public: strstreambuf::~strstreambuf();
The public strstreambuf destructor destroys the strstreambuf object after discarding the reserve area.
The reserve area is discarded only if the strstreambuf object is using dynamic allocation and isn't frozen. The reserve area is freed using the free function specified by the form of the constructor that allows specification of the allocate and free functions, or using the operator delete() intrinsic function.
If the strstreambuf object is frozen or is using static allocation, the user of the strstreambuf object must have a pointer to the reserve area, and is responsible for freeing it.
The call to the public strstreambuf destructor is inserted implicitly by the compiler at the point where the strstreambuf object goes out of scope.
The strstreambuf object is destroyed.
does nothing
#include <strstrea.h> public: virtual int strstreambuf::sync();
The sync() public virtual member function does nothing because there's no external device with which to synchronize.
The sync() public virtual member function returns __NOT_EOF.
extend the get area, if possible
#include <strstrea.h> public: virtual int strstreambuf::underflow();
The underflow() public virtual member function provides the input communication between the streambuf member functions and the strstreambuf object. Member functions in the streambuf class call the underflow() public virtual member function when the get area is empty.
If there's a non-empty put area present following the get area, the get area is extended to include the put area, allowing the input operation to continue using the put area. Otherwise the get area can't be extended.
The underflow() public virtual member function returns the first available character in the get area on successful extension, otherwise EOF is returned.