Notes on file I/O: Predefined objects used with I/O operators: cin is instance of istream tied to standard input device (keyboard) cout is instance of ostream tied to standard output device (monitor) also cerr (unbuffered ostream object) for error output messages g++ -o mypgm.exe mypgm.cpp >& myout.txt get both outputs endl stream manipulator both issues '\n' and causes buffer output to be written immediately can also force write of output buffer with flush manipulator cout << flush; get member functions (of istream) has three versions get with no arguments, inputs one character from designated stream (even if whitespace), and returns character (including EOF) get with char reference argument inputs next character and stores it in the char argument get with three arguments (char array, size limit and delimeter with default of '\n') reads characters from the input stream up to one less than specified size limit or until delimiter is reached, a null character is added, and the resulting null-terminated string stored in the char array; delimiter remains in input stream getline like last version of get above, but delimiter is read from (and removed) from stream, but not added to the array Unformatted I/O Unformatted I/O performed with read and write member functions Bytes are read as bytes and stored in memory; a byte is not considered part of a larger unit (like an int) write requires address and length arguments: cout.write(myString, 15); read inputs designated number of bytes into address specified (usually an array) if fewer than designated number of bytes read, failbit is set member function gcount returns number of bytes read by the last operation Stream Manipulators * Stream manipulators perform formatting tasks * header file must be included for parameterized stream manipulators * Setting base of integers o hex, oct, and dec are manipulators o setbase is parameterized stream manipulator, requires argument of 8, 10, or 16 (for octal, decimal, and hex) o stream base is persistent * Floating point o determines number of digits to the right of the decimal point o precision is member function (cout.precision(n)) o setprecision is parameterized manipulator cout << setprecision(n)) o stream precision is persistent o precision with no arguments returns current precision n = cout.precision() * Field width o sets width of field for next input (if character array) or output o width is member function of ios cin.width(n) or cout.width(n)) o setw is manipulator cout << setw(n)) o field width is not persistent Stream Format States * There are format flags associated with each stream that determine the type of formatting to be performed during I/O operations * member functions setf, unsetf, and flags control flag settings * can also use stream manipulator setiosflags and resetiosflags * can set many flags at once by combining with the bitwise-or operator ( | ) into a long o flags() sets all flags on a stream and returns long value containing previous options, so that they can be saved and reset ios_base::fmtflags originalFormat = cout.flags(); cout << ... ; // change format cout.flags(originalFormat); // restore format o setf can set one or more flags (ORs with existing flag settings) * Trailing zeros and decimal points (ios::showpoint): o forces display of decimal point (and trailing zeros according to current precision) for "integer" values (i.e., the value 23.00 displays as just 23 without this flag set) * Justification within a field (ios::left, ios::right, ios::internal) o left-justify, right-justify, or fill number between sign and value with pad character o when using setf, must use ios::adjustfield as second argument: cout.setf(ios::left, ios::adjustfiled) o fill member function (or setfill manipulator) specifies pad character * Padding (fill, setfill) o note also use of ios::showbase to force display of integer base (none for decimal, 0 for octal, 0x for hex) * Floating point number format o use with precision o to reset to default: cout.setf(0, ios::floatfield); * Upper/lowercase o only for uppercase X and E in hex, scientific notation, and all hex numerals (A-F) Stream Error States * State of a stream can be tested through bits in class ios eofbit is set (=1) after EOF is encountered can use eof function to test this bit failbit is set for a stream when a format error occurs (e.g. a non-digit character is encountered while inputting an integer) test with fail() badbit set when error occurs that results in loss of data nonrecoverable test with bad() goodbit is set if eofbit, failbit, and badbit are not set test with good() rdstate member function returns error state of the stream, which can then be tested (using a switch that examines each bit) clear() member function resets specific bit, or sets goodbit if no argument given Tying an Output Stream to an Input Stream Normally each stream is treated independently If streams are buffered, input/output happens only when buffers are full In some cases, input and output should be "paired" (or synchronized) so than output happens before input, regardless of buffer state (as in a prompt for subsequent input) tie function used to do this: cin.tie( &cout ); // ties cin to cout Above done automatically in C++, but other istream/ostream pairs would need to be explicitly tied by user.