This section defines the ostream functions for formatted and unformatted output. #include <iostream.h> typedef long streamoff, streampos; class ios enum seek_dir {beg, cur, end}; bin, tabexp}; enum { skipws=01, }; // see ios for other class members } ; class ostream : virtual public ios { public:
}; class ostream_withassign : public ostream { public:
}; extern ostream_withassign cout;
| |||||||||||||
ostreams support insertion (storing) into a streambuf. These are commonly referred to as output operations. The ostream member functions and related functions are In the following descriptions, assume:
Constructors and assignmentostream(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 functionint 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 functionvoid 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 char* any integral type
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 functionsostream * 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 functionsostream * 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. 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 functionsostream * 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. Manipulatorsouts<<endl Ends a line by inserting a newline character and flushing. 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 | ||||||||||||
SEE ALSO | |||||||||||||