Presentation is loading. Please wait.

Presentation is loading. Please wait.

IOStreams CNS 3370 Copyright 2003, Fresh Sources, Inc.

Similar presentations


Presentation on theme: "IOStreams CNS 3370 Copyright 2003, Fresh Sources, Inc."— Presentation transcript:

1 IOStreams CNS 3370 Copyright 2003, Fresh Sources, Inc.

2 Agenda Inserters and Extractors Stream State Files Streams String Streams Formatting Manipulators Internationalization

3 Inserters Inserts an object into a stream –that is, it does output Uses operator<< –the left-shift operator –the arrow suggest the direction of the data flow Easy to define for your own classes

4 Inserter Example A Date class inserter: ostream& operator<<(ostream& os, const Date& d) { char fillc = os.fill('0'); os << setw(2) << d.getMonth() << '-' << setw(2) << d.getDay() << '-' << setw(4) << d.getYear() << setfill(fillc); return os; }

5 Extractor Example istream& operator>>(istream& is, Date& d) { is >> d.month; char dash; is >> dash; if(dash != '-') { is.putback(dash); is.setstate(ios::failbit); } is >> d.day; is >> dash; if(dash != '-') { is.putback(dash); is.setstate(ios::failbit); } is >> d.year; return is; }

6 Stream State 4 states: –good –eof –fail (unexpected input type, like alpha for numeric) also set by eof –bad (device failure) Once a stream is no longer good, you can’t use it –all ops are no-ops –Can clear with clear( ) (must after a failed op!) Can test with associated functions: –good( ), eof( ), fail( ), bad( ) Can test for good( ) like this: –if (theStream) [same as if (theStream.good( ))]

7 Streams and Exceptions Can have exceptions thrown instead of checking state Call the exceptions( ) member function –Can pick which states you want to throw: myStream.exceptions(ios::badbit); The exception type thrown is ios::failure –we’ll see the ios base class later

8 File Streams Classes ifstream, ofstream, fstream –declared in Constructors open, destructors close All normal stream operations apply Additional member functions: –close( ), open( ) Open modes –ios::in, ios::out, ios::app, ios::ate, ios::trunc, ios::binary –Can combine with the bitwise or ( | )

9 Stream Buffers The data area(s) held by the stream –One for input, one for output (as declared) –Streams that support both, have both –Can access via the function rdbuf( ) A “Way Station” for data en route Usually don’t worry about it One cool feature: –SType.cpp

10 Stream Positioning Can move around in a stream –except the console, of course Using functions seekp( ), seekg( ) –seekp( ) seeks in the output buffer (“put”) –seekg( ) seeks in the input buffer (“get”) –Simultaneous I/O streams share the same buffer File streams keep the put/get pointers together In string streams they’re independent Example: IOFile.cpp

11 Sharing a Buffer Between Streams Multiple streams can process the same source or target Achieved by sharing a stream buffer See hexdec.cpp

12 String Streams Classes istringstream, ostringstream, stringstream –declared in Writes to or reads from a string –or both but remember the get/put pointers are independent Useful for converting other data types to and from strings Examples: IString.cpp, Ostring.cpp, HTMLStripper2.cpp

13 Output Formatting Can set stream attributes –width, fill character, alignment, numeric base, floating- point format, decimal precision, etc. Use setf( ) and unsetf( ) Example: Format.cpp

14 Manipulators A shorthand for setting/unsetting stream attributes –dec, hex, endl, flush Achieved via a special overload convention –manipulators are functions –when inserted, the following function is called: ostream& ostream::operator<<(ostream& (*pf)(ostream&)) { return pf(*this); } The function pf should do its work and return the stream

15 Creating a Manipulator #include Define a function with the required signature Do your work and return the stream: ostream& nl(ostream& os) { return os << '\n'; } int main() { cout << "newlines" << nl << "between" << nl << "each" << nl << "word" << nl; } cout << nl becomes cout.operator<<(nl), which executes nl(cout), which executes cout << ‘\n’;

16 Manipulators with Arguments setw(n), setfill(c), setprecision(n), etc. Must include for these Example: Manips.cpp Difficult to implement your own –not portable Use Effectors instead

17 Effectors Create a class whose constructor formats a string according to its purpose That class also provides an operator<< Example: Effector.cpp

18 Wide Streams The streams we’ve been using traffic in bytes (char) You can have streams that use wide characters (wchar_t) –displaying foreign characters requires platform support outside of C++ –C++ just stores code points

19 basic_istream The template that governs the standard stream classes: template > class basic_istream {...}; typedef basic_istream istream; typedef basic_istream wistream; typedef basic_ifstream ifstream; typedef basic_ifstream wifstream; typedef basic_istringstream istringstream; typedef basic_istringstream wistringstream;

20 Locales Cultural customization of I/O formatting A stream has an associated locale Can change it with imbue( ) Example: Locale.cpp (.NET only) Java’s locale support is MUCH better


Download ppt "IOStreams CNS 3370 Copyright 2003, Fresh Sources, Inc."

Similar presentations


Ads by Google