Main Page

Previous Page
Next Page

[Page 800 (continued)]


  • I/O operations are performed in a manner sensitive to the type of the data.

  • C++ I/O occurs in streams. A stream is a sequence of bytes.

  • I/O mechanisms of the system move bytes from devices to memory and vice versa efficiently and reliably.

  • [Page 801]
  • C++ provides "low-level" and "high-level" I/O capabilities. Low-level I/O-capabilities specify that some number of bytes should be transferred device-to-memory or memory-to-device. High-level I/O is performed with bytes grouped into such meaningful units as integers, floats, characters, strings and user-defined types.

  • C++ provides both unformatted-I/O and formatted-I/O operations. Unformatted-I/O transfers are fast, but process raw data that is difficult for people to use. Formatted I/O processes data in meaningful units, but requires extra processing time that can degrade the performance of high-volume data transfers.

  • The <iostream> header file declares all stream-I/O operations.

  • Header <iomanip> declares the parameterized stream manipulators.

  • The <fstream> header declares file-processing operations.

  • The basic_istream template supports stream-input operations.

  • The basic_ostream template supports stream-output operations.

  • The basic_iostream template supports both stream-input and stream-output operations.

  • The basic_istream template and the basic_ostream template are each derived through single inheritance from the basic_ios template.

  • The basic_iostream template is derived through multiple inheritance from both the basic_istream template and the basic_ostream template.

  • The left-shift operator (<<) is overloaded to designate stream output and is referred to as the stream insertion operator.

  • The right-shift operator (>>) is overloaded to designate stream input and is referred to as the stream extraction operator.

  • The istream object cin is tied to the standard input device, normally the keyboard.

  • The ostream object cout is tied to the standard output device, normally the screen.

  • The ostream object cerr is tied to the standard error device. Outputs to cerr are unbuffered; each insertion to cerr appears immediately.

  • The C++ compiler determines data types automatically for input and output.

  • Addresses are displayed in hexadecimal format by default.

  • To print the address in a pointer variable, cast the pointer to void *.

  • Member function put outputs one character. Calls to put may be cascaded.

  • Stream input is performed with the stream extraction operator >>. This operator automatically skips white-space characters in the input stream.

  • The >> operator returns false after end-of-file is encountered on a stream.

  • Stream extraction causes failbit to be set for improper input and badbit to be set if the operation fails.

  • A series of values can be input using the stream extraction operation in a while loop header. The extraction returns 0 when end-of-file is encountered.

  • The get member function with no arguments inputs one character and returns the character; EOF is returned if end-of-file is encountered on the stream.

  • Member function get with a character-reference argument inputs the next character from the input stream and stores it in the character argument. This version of get returns a reference to the istream object for which the get member function is being invoked.

  • Member function get with three argumentsa character array, a size limit and a delimiter (with default value newline)reads characters from the input stream up to a maximum of limit 1 characters and terminates, or terminates when the delimiter is read. The input string is terminated with a null character. The delimiter is not placed in the character array but remains in the input stream.

    [Page 802]
  • The getline member function operates like the three-argument get member function. The getline function removes the delimiter from the input stream but does not store it in the string.

  • Member function ignore skips the specified number of characters (the default is 1) in the input stream; it terminates if the specified delimiter is encountered (the default delimiter is EOF).

  • The putback member function places the previous character obtained by a get on a stream back onto that stream.

  • The peek member function returns the next character from an input stream but does not extract (remove) the character from the stream.

  • C++ offers type-safe I/O. If unexpected data is processed by the << and >> operators, various error bits are set, which the user may test to determine whether an I/O operation succeeded or failed. If operator << has not been overloaded for a user-defined type, a compiler error is reported.

  • Unformatted I/O is performed with member functions read and write. These input or output some number of bytes to or from memory, beginning at a designated memory address. They are input or output as raw bytes with no formatting.

  • The gcount member function returns the number of characters input by the previous read operation on that stream.

  • Member function read inputs a specified number of characters into a character array. failbit is set if fewer than the specified number of characters are read.

  • To change the base in which integers output, use the manipulator hex to set the base to hexadecimal (base 16) or oct to set the base to octal (base 8). Use manipulator dec to reset the base to decimal. The base remains the same until changed explicitly.

  • The parameterized stream manipulator setbase also sets the base for integer output. setbase takes one integer argument of 10, 8 or 16 to set the base.

  • Floating-point precision can be controlled using either the setprecision stream manipulator or the precision member function. Both set the precision for all subsequent output operations until the next precision-setting call. The precision member function with no argument returns the current precision value.

  • Parameterized manipulators require the inclusion of the <iomanip> header file.

  • Member function width sets the field width and returns the previous width. Values narrower than the field are padded with fill characters. The field-width setting applies only for the next insertion or extraction; the field width is set to 0 implicitly (subsequent values will be output as large as necessary). Values wider than a field are printed in their entirety. Function width with no argument returns the current width setting. Manipulator setw also sets the width.

  • For input, the setw stream manipulator establishes a maximum string size; if a larger string is entered, the larger line is broken into pieces no larger than the designated size.

  • Programmers may create their own stream manipulators.

  • Stream manipulator showpoint forces a floating-point number to be output with a decimal point and with the number of significant digits specified by the precision.

  • Stream manipulators left and right cause fields to be left justified with padding characters to the right or right justified with padding characters to the left.

  • Stream manipulator internal indicates that a number's sign (or base when using stream manipulator showbase) should be left justified within a field, its magnitude should be right justified and intervening spaces should be padded with the fill character.

  • [Page 803]
  • Member function fill specifies the fill character to be used with stream manipulators left, right and internal (space is the default); the prior padding character is returned. Stream manipulator setfill also sets the fill character.

  • Stream manipulators oct, hex and dec specify that integers are to be treated as octal, hexadecimal or decimal values, respectively. Integer output defaults to decimal if none of these bits is set; stream extractions process the data in the form the data is supplied.

  • Stream manipulator showbase forces the base of an integral value to be output.

  • Stream manipulator scientific is used to output a floating-point number in scientific format. Stream manipulator fixed is used to output a floating-point number with the precision specified by the precision member function.

  • Stream manipulator uppercase forces an uppercase X or E to be output with hexadecimal integers or with scientific-notation floating-point values, respectively. When set, uppercase causes all letters in a hexadecimal value to be uppercase.

  • Member function flags with no argument returns the long value of the current format state settings. Function flags with a long argument sets the format state specified by the argument.

  • The state of a stream may be tested through bits in class ios_base.

  • The eofbit is set for an input stream after end-of-file is encountered during an input operation. The eof member function reports whether the eofbit has been set.

  • The failbit is set for a stream when a format error occurs on the stream. The fail member function reports whether a stream operation has failed; it is normally possible to recover from such errors.

  • The badbit is set for a stream when an error occurs that results in data loss. The bad member function reports whether such a stream operation failed. Such serious failures are normally non-recoverable.

  • The good member function returns true if the bad, fail and eof functions would all return false. I/O operations should be performed only on "good" streams.

  • The rdstate member function returns the error state of the stream.

  • Member function clear restores a stream's state to "good," so that I/O may proceed on that stream.

  • C++ provides the tie member function to synchronize istream and ostream operations to ensure that outputs appear before subsequent inputs.

Previous Page
Next Page