The GNU C++ Iostream Library



3.2: Managing output streams: class ostream

Objects of class ostream inherit the generic methods from ios, and in addition have the following methods available. Declarations for this class come from iostream.h.


  • Constructors Creating an ostream.
  • Writing Writing on an ostream.
  • Output Position Repositioning an ostream.
  • Ostream Housekeeping Miscellaneous ostream utilities.


  • 3.2.1: Constructors


    Constructor:  ostream::ostream ()

    The simplest form of the constructor for an ostream simply allocates a new ios object.

    Constructor:  ostream::ostream (streambuf* sb [, ostream tie])

    This alternative constructor requires a first argument sb of type streambuf*, to use an existing open stream for output. It also accepts an optional second argument tie, to specify a related ostream* as the initial value for ios::tie.

    If you give the ostream a streambuf explicitly, using this constructor, the sb is not destroyed (or deleted or closed) when the ostream is destroyed.


    3.2.2: Writing on an ostream

    These methods write on an ostream (you may also use the operator <<; see Operators).


    Method:  ostream& ostream::put (char c)

    Write the single character c.

    Method:  ostream& ostream::write (string, int length)

    Write length characters of a string to this ostream, beginning at the pointer string.

    string may have any of these types: char*, unsigned char*, signed char*.


    Method:  ostream& ostream::form (const char *format, ...)

    A GNU extension, similar to fprintf(file, format, ...).

    format is a printf-style format control string, which is used to format the (variable number of) arguments, printing the result on this ostream. See ostream::vform for a version that uses an argument list rather than a variable number of arguments.


    Method:  ostream& ostream::vform (const char *format, va_list args)

    A GNU extension, similar to vfprintf(file, format, args).

    format is a printf-style format control string, which is used to format the argument list args, printing the result on this ostream. See ostream::form for a version that uses a variable number of arguments rather than an argument list.


    3.2.3: Repositioning an ostream

    You can control the output position (on output streams that actually support positions, typically files) with these methods:


    Method:  streampos ostream::tellp ()

    Return the current write position in the stream.

    Method:  ostream& ostream::seekp (streampos loc)

    Reset the output position to loc (which is usually the result of a previous call to ostream::tellp). loc specifies an absolute position in the output stream.

    Method:  ostream& ostream::seekp (streamoff loc, rel)

    Reset the output position to loc, relative to the beginning, end, or current output position in the stream, as indicated by rel (a value from the enumeration ios::seekdir):
    beg
    Interpret loc as an absolute offset from the beginning of the file.
    cur
    Interpret loc as an offset relative to the current output position.
    end
    Interpret loc as an offset from the current end of the output stream.


    3.2.4: Miscellaneous ostream utilities

    You may need to use these ostream methods for housekeeping:


    Method:  ostream& flush ()

    Deliver any pending buffered output for this ostream.

    Method:  int ostream::opfx ()

    opfx is a prefix method for operations on ostream objects; it is designed to be called before any further processing. See ostream::osfx for the converse.

    opfx tests that the stream is in state good, and if so flushes any stream tied to this one.

    The result is 1 when opfx succeeds; else (if the stream state is not good), the result is 0.


    Method:  void ostream::osfx ()

    osfx is a suffix method for operations on ostream objects; it is designed to be called at the conclusion of any processing. All the ostream methods end by calling osfx. See ostream::opfx for the converse.

    If the unitbuf flag is set for this stream, osfx flushes any buffered output for it.

    If the stdio flag is set for this stream, osfx flushes any output buffered for the C output streams stdout and stderr.



    Translated 02/24/96 by Rance Necaise. Original texi file by >Bothner and Pesch.