Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

ostream Formatted and unformatted output


This section defines the ostream functions for formatted and unformatted output.


#include <iostream.h>

typedef long streamoff, streampos;

class ios
{
public:

enum seek_dir {beg, cur, end};
enum open_mode {in, out, ate, app, trunc, nocreate, noreplace,

bin, tabexp};

enum

{

skipws=01,
left=02, right=04, internal=010,
dec=020, oct=040, hex=0100,
showbase=0200, showpoint=0400,
uppercase=01000, showpos=02000,
scientific=04000, fixed=010000,
unitbuf=020000, stdio=040000

};

//  see ios for other class members

} ;

class ostream : virtual public ios

{

public:


ostream(streambuf*);

virtual

ostream&
int
void
ostream&
ostream&
ostream&
streampos
ostream&
ostream&
ostream&
ostream&
ostream&
ostream&
ostream&
ostream&
ostream&
ostream&
ostream&
ostream&
ostream&
ostream&
ostream&
ostream&
ostream&

˜ostream();

flush();
opfx();
osfx();
put(char);
seekp(streampos);
seekp(streamoff, ios::seek_dir);
tellp();
write(const char* ptr, int n);
write(const unsigned char* ptr, int n);
operator<<(const char*);
operator<<(char);
operator<<(short);
operator<<(int);
operator<<(long);
operator<<(float);
operator<<(double);
operator<<(unsigned char);
operator<<(unsigned short);
operator<<(unsigned int);
operator<<(unsigned long);
operator<<(void*);
operator<<(streambuf*);
operator<<(ostream& (*)(ostream&));
operator<<(ios& (*)(ios&));

};

class ostream_withassign : public ostream

{

public:


ostream_withassign();

virtual

˜ostream_withassign();

ostream_withassign&
ostream_withassign&

operator=(ostream&);
operator=(streambuf*);

};

extern ostream_withassign cout;
extern ostream_withassign cerr;
extern ostream_withassign clog;

ostream&
ostream&
ostream&
ios&
ios&
ios&

endl(ostream&);
ends(ostream&);
flush(ostream&);
dec(ios&);
hex(ios&);
oct(ios&);





ostreams support insertion (storing) into a streambuf. These are commonly referred to as output operations. The ostream member functions and related functions are
described below.

In the following descriptions, assume:

  • outs is an ostream.
  • outswa is an ostream_withassign.
  • outsp is an ostream*.
  • c is a char.
  • ptr is a char* or unsigned char*.
  • sb is a streambuf*
  • i and n are int
  • pos is a streampos.

  • off is a streamoff.

  • dir is a seek_dir.

  • manip is a function with type ostream& (*)(ostream&).

Constructors and assignment

ostream(streambuf * sb)

Initializes ios and ostream state variables and associates buffer sb with the ostream.

ostream_withassign()

Does no initialization. This allows a file static variable of this type (cout for example) to be used before it is constructed, provided it is assigned to first.

outswa=sb

Associates sb with outswa and initializes the entire state of outswa.

outswa=outs

Associates outs.rdbuf() with outswa and initializes the entire state of outswa.

Output prefix function

int outs.opfx()

If outs’s error state is non-zero, returns zero immediately. If outs.tie() is non-null, the ioss associated with outs are flushed. Returns non-zero in all other cases.

Output suffix function

void osfx()

Performs "suffix" actions before returning from inserters. If ios::unitbuf is set, osfx() flushes the ostream. If ios::stdio is set, osfx() flushes stdout and stderr. Under BS2000, flushing stdio and stderr implies, among other things, that the current line (record) is terminated. Subsequent data is written to the next line.

osfx() is called by all predefined inserters, and should be called by user-defined inserters as well, after any direct manipulation of the streambuf. It is not called by the binary output functions.

Formatted output functions (inserters)

outs<<x

First calls outs.opfx() and if that returns 0, does nothing. Otherwise inserts a
sequence of characters representing x into outs.rdbuf(). Errors are indicated by
setting the error state of outs. outs is always returned.
x is converted into a sequence of characters (its representation) according to rules that depend on x’s type and outs’s format state flags and variables (see ios). Inserters are defined for the following types, with conversion rules as described below:

char*
The representation is the sequence of characters up to (but not including) the
terminating null of the string x points at.

any integral type
(except char and unsigned char)

  • If x is positive, the representation contains a sequence of decimal, octal, or hexadecimal digits with no leading zeros, depending on whether ios::dec, ios::oct, or ios::hex is set in ios’s format flags. If none of those flags are set, conversion defaults to decimal.
  • If x is zero, the representation is a single zero character(0).
  • If x is negative, decimal conversion converts it to a minus sign (-) followed by decimal digits.
  • If x is positive and ios::showpos is set, decimal conversion converts it to a plus sign (+) followed by decimal digits. The other conversions treat all values as unsigned. If ios::showbase is set in ios’s format flags, the hexadecimal representation contains 0x before the hexidecimal digits, or 0X if ios::uppercase is set. If ios::showbase is set, the octal representation contains a leading 0.

void*

Pointers are converted to integral values and then converted to hexadecimal numbers as if ios::showbase were set.

float, double

The arguments are converted according to the current values of outs.precision()outs.width() and outs’s format flags ios::scientific, ios::fixed, and ios::uppercase (see ios).  The default value for outs.precision() is 6. If neither ios::scientific nor ios::fixed is set, either fixed or scientific notation is chosen for the representation, depending on the value of x.

char, unsigned char

No special conversion is necessary.

After the representation is determined, padding occurs. If outs.width() is greater than0 and the representation contains fewer than outs.width() characters, then enough outs.fill() characters are added to bring the total number of characters to ios.width().

If ios::left is set in ios’s format flags, the sequence is left-adjusted, that is, charactersare added after the characters determined above. If ios::right is set, the padding is added before the characters determined above. If ios::internal is set, the padding is added after any leading sign or base indication and before the characters that represent the value. ios.width() is reset to 0, but all other format variables are unchanged. The resulting sequence (padding plus representation) is inserted into outs.rdbuf().

outs<<sb

If outs.opfx() returns non-zero, the sequence of characters that can be fetched from sb are inserted into outs.rdbuf(). Insertion stops when no more characters can be fetched from sb. No padding is performed. Always returns outs.

Unformatted output functions

ostream * outsp=&outs.put(char c)

Inserts c into outs.rdbuf(). Sets the error state if the insertion fails.

ostream * outsp=&outs.write(char * s, int n)

Inserts the n characters starting at s into outs.rdbuf(). These characters may include zero bytes (i.e., s need not be a null-terminated string).

Other member functions

ostream * outsp=&outs.flush()

Storing characters into a streambuf does not always cause them to be consumed (e.g., written to the external file) immediately. flush() causes any characters that mayhave been stored but not yet consumed to be consumed by calling outs.rdbuf()->sync.
In BS2000, this means that these characters are passed to the C runtime system.

outs<<manip

Equivalent to manip(outs). Syntactically this looks like an insertion operation, but semantically it does an arbitrary operations rather than converting manip to a sequence of characters as do the insertion operators. Predefined manipulators are described below.

Positioning functions

ostream * outsp=&outs.seekp(streamoff off, ios::seek_dir dir)

Repositions outs.rdbuf()’s put pointer. See sbufpub for a discussion of positioning.

ostream * outsp=&outs.seekp(streampos pos)

Repositions outs.rdbuf()’s put pointer. See sbufpub for a discussion of positioning.

streampos pos=outs.tellp()

The current position of outs.rdbuf()’s put pointer. See sbufpub for a discussion of positioning.

Manipulators

outs<<endl

Ends a line by inserting a newline character and flushing.
Under BS2000, the newline character is converted to a change-of-record character.

outs<<ends

Ends a string by inserting a null(0) character.

outs<<flush

Flushes outs.

outs<<dec

Sets the conversion base format flag to 10. See ios.

outs<<hex

Sets the conversion base format flag to 16. See ios.

outs<<oct

Sets the conversion base format flag to 8. See ios.

EXAMPLE

The following program displays a range of different data types in a variety of different formats:

#include <iostream.h>
#include <iomanip.h>  /* for setw */
int main()
{
  int i = 50;
  char c = 'd';
  double d = 1.2;
  float f = 3.1232;
  const char * const p = "abcdefghijklmnopqrstuvwxyz";
  /* show the defaults for the various data types first  */
  cout << i << endl;
  cout << c << endl;
  cout << d << endl;
  cout << f << endl;
  cout << p << endl;
  cout << endl;
  cout.setf( ios::oct, ios::basefield);
  cout << i << endl;  /* same number in octal */
  cout << c << endl;
  cout.setf( ios::fixed, ios::floatfield);
  /* use fixed format for floats and doubles */
  cout << d << endl;
  cout << f << endl;  /* above format still holds */
  cout.setf( ios::right, ios::basefield);
  cout << setw( 50) << flush;
  cout << p << endl;  /* put string in field of width 50 */
  return 0;
}

The result of executing the program is:

50
d
1.2
3.1232
abcdefghijklmnopqrstuvwxyz
62
d
1.200000
3.123199
                        abcdefghijklmnopqrstuvwxyz
%  CCM0998 CPU time used: 0.0009 seconds
Note how the integer i has been printed out in two different formats, and the ease by which the format of doubles and floats can be controlled. As shown in the first part of main(), the output library provides sensible defaults, without the programmer explicitly setting them up.

SEE ALSO


ios, manip, sbufpub