Logistics • Project Basic IOStreams – Part 2 (water) due Sunday, Oct 16 th • Feedback by Monday – Part 3 (block) due Sunday, Oct 30 • Questions? Exam Logistics • Exam 2 • Final exam – Thursday, October 27 – Good news…bad news – Good news • Last day of finals, November 18 th – More details on Thursday. – Bad news • 8am-10am – Room • 01-3338 Plan for this week IOStreams • I/O Week • Suite of classes for performing I/O in C++ – Today: IOStreams 1 • Reading and Writing: – Tomorrow: IOStreams 2 – Standard input and output – Thursday: IOStreams 3 – File input and output – Input and Output to strings 1
Streams Streams • Like Java, Basic low level mechanism for I/O is • Unlike Java, the basic stream in C++ is the stream buffered. – Stream is a sequence of bytes – Used to increase efficiency – When the program writes something, it is put into a buffer in memory. – The output doesn't appear on the screen until the buffer is flushed (written) IOStream class inheritance IStream • Extraction cin is an istream – Input from an istream object is called extraction. – pulling characters out of the stream and into your program cout and cerr – istream manages format conversion are ostreams – istream maintains an error state • EOF reached Other classes • Format errors inherit from these • Serious errors and add I/O – Two types of extraction to/from a device, • Formatted / Unformatted string, or memory Formatted Extraction Formatted Extraction • Done via operator >> • Four steps of a formatted extraction 1. The stream’s error state is checked. If it is nonzero int i; (indicating EOF or some error occurred), the cin >> i; remaining steps are skipped • operator>> overloaded for different datatypes. 2. If the extraction is from cin , then cout is flushed. – C++ provides operator>> for basic datatypes 3. Leading whitespace is skipped. – You can write your own for classes 4. Characters are extracted as needed to obtain the – operator>> returns a reference to an istream desired value. Whitespace terminates the extraction int i, j, k; cin >> i >> j >> k; 2
Formatted Extraction Formatted Extraction • Testing the error state: • The error state – cin.good() – Three error bits • Returns true if none of the error bits are set • eofbit – end of file was reached – cin.eof() • failbit – an extraction failed (format error, • Returns true if eofbit is set premature EOF) – cin.fail() • badbit – the stream is bad and probably can’t be used any more • Returns true failbit or badbit is set – cin.bad() • Returns true if badbit is set Formatted Extraction Formatted Extraction • Testing the error state. • About EOF – istream overrides some operators to allow an istream to – EOF is not set when you’ve read the last be tested as a boolean: character from the stream • if( cin ) is the same as if( !cin.fail() ) – it is set when you try to read one character past • if( !cin ) is the same as if( cin.fail()) the end. – One can also write • if( cin >> i ) – If EOF is encountered while skipping leading – Which is equivalent to white space, failbit is set. • cin >> i; • if( !cin.fail() ) Formatted Extraction Formatted Extraction • Must test stream after extraction • Example – A good stream does not guarantee more input – sp sp 123 nl – cin >> i; // all is fine cin >> i; while( !cin.fail() ){ // process the value in i – sp sp 123 cin >> i; } – cin >> i; // extraction success but eofbit // will be set…next read will Or fail while( cin >> i ){ // process the value in i } 3
Formatted Extraction Formatted Extraction • But not this: • Extraction of int datatypes – Rules: • If the value begins with 0, it is assumed to be an while( cin ){ octal number. cin >> i; • If the value begins with 0x or 0X, it is assumed to be // process the value in i a hexadecimal number. } • Otherwise, the value is assumed to be a decimal number. Formatted Extraction Formatted Extraction • Example: • Extraction of int datatypes – will fail for date 09/10/02 (since 09 will be interpreted – You can force a particular conversion by as octal) • Setting the format flag in your ios_base (ios:dec, char slash; int month, day, year; ios:hex, ios:oct) cin >> month >> slash >> day >> slash >> year; • Send a manipulator (dec, hex, oct) to the istream – Use instead: cin >> dec >> month >> slash >> day >> slash >> year; Questions? Unformatted Extraction Unformatted Extraction • Allows for reading of byte data directly • Read one character at a time • Formatted vs. Unformatted ch = cin.get(); 1. No conversion is done in unformatted while( ch != EOF ){ extractions: bytes are copied, that is all. // process the character ch = cin.get(); 2. Leading white space is not skipped. } 3. Calls are made to member functions rather OR than overloaded operators. while( cin.get( ch ) ){ // process the character 1. get(), getline(), read() } 4
Unformatted Extraction Unformatted Extraction • Read multiple chars • Reading raw bytes #define BUFLEN ... #define N_VALUES ... char buffer[ BUFLEN ]; char values[ N_VALUES ]; // reads up to the first , or until BUFLEN chars cin.read( values, N_VALUES ); // bytes are read cin.get( buffer, BUFLEN, ’,’ ); into // char arrays OR // get chars a line at a time while( cin.getline( buffer, BUFLEN ) ){ Questions? // process the line in buffer } OStream Formatted Insertion • Done via operator << • Insertion – Output to an ostream is called an insertion int i = 7; cout << i; – Output to an ostream is called an insertion • operator<< overloaded for different datatypes. – ostream manages format conversion – C++ provides operator<< for basic datatypes – ostream maintains an error state but it’s not – You can write your own for classes as important as it is with input – operator>> returns a reference to an ostream – Two types of extraction int i, j, k; cout << i << j << k; • Formatted / Unformatted Formatted Insertion Formatted Insertion • Steps of a formatted insertion • Format state is more important than with input 1. The stream’s error state is checked. If failbit or badbit are set, the remaining – The format state consists of steps are skipped • a number of flags (default: none set) 2. The value is converted to the appropriate • the fill character (default: ‘ ‘) characters, possibly padded, and then inserted • Precision (default 6) into the stream. • Mimimum Width(default 0) 5
Formatted Insertion Formatted Insertion enum fmt_flags { • Format flags boolalpha = 0x0001, dec = 0x0002, int flags(); // returns flag settings fixed = 0x0004, hex = 0x0008, int flags (int f); // replaces current settings internal = 0x0010, int setf (int f); // turns on flags left = 0x0020, int unsetf (int f); // turns off flags oct = 0x0040, right = 0x0080, scientific = 0x0100, showbase = 0x0200, showpoint = 0x0400, showpos = 0x0800, skipws = 0x1000, unitbuf = 0x2000, uppercase = 0x4000 }; Formatted Insertion Formatted Insertion • Manipulators • Manipulating format state: – cout << dec; char fill (); // returns the fill char • Equivalent to cout.setf( ios::dec ); char fill (char c); // sets the fill char – cout << oct; int precision (); // returns the precision • Equivalent to cout.setf( ios::oct ); int precision (int p); // sets the precision – cout << hex; int width (); // returns the width • Equivalent to cout.setf( ios::hex ); int width (int w); // sets the width – cout << flush; • Flushes the output buffer – cout << endl; • Ends a line by inserting a newline and flushing the output buffer Formatted Insertion Unformatted Insertion • Manipulator • Allows for writing of byte data directly – cout << setw( w ); • For single byte: • Sets the width to w (affects only next << ) – cout.put (ch); – cout << setfill( c ); • Sets the fill character to c (affects all <<) • For array of bytes – cout << setprecision( p ); – Sets the precision to p (affects all << ) – cout.write (buffer, len); – cout << setiosflags( f ); • Equivalent to cout.setf( f ); – cout << resetiosflags( f ); • Equivalent to cout.unsetf( f ); 6
No wait…there’s more IOStream Class Hierarchy • Reading/Writing from/to files and strings – fstream – I/O to/from files – sstream – I/O to and from strings …but we’ll leave that until tomorrow • Questions? 7
Recommend
More recommend