CS 3370. Inserters and Extractors Stream State Files Streams String Streams Formatting ...
-
Upload
melina-tate -
Category
Documents
-
view
226 -
download
0
description
Transcript of CS 3370. Inserters and Extractors Stream State Files Streams String Streams Formatting ...
![Page 1: CS 3370. Inserters and Extractors Stream State Files Streams String Streams Formatting Manipulators Internationalization.](https://reader035.fdocuments.in/reader035/viewer/2022062523/5a4d1adf7f8b9ab059976692/html5/thumbnails/1.jpg)
CS 3370
![Page 2: CS 3370. Inserters and Extractors Stream State Files Streams String Streams Formatting Manipulators Internationalization.](https://reader035.fdocuments.in/reader035/viewer/2022062523/5a4d1adf7f8b9ab059976692/html5/thumbnails/2.jpg)
Inserters and ExtractorsStream StateFiles StreamsString StreamsFormattingManipulators Internationalization
![Page 3: CS 3370. Inserters and Extractors Stream State Files Streams String Streams Formatting Manipulators Internationalization.](https://reader035.fdocuments.in/reader035/viewer/2022062523/5a4d1adf7f8b9ab059976692/html5/thumbnails/3.jpg)
Insert an object into a stream it does formatted output
Uses operator<< the “left-shift” operator the arrow suggests the direction of the
data flowEasy to define for your own classes
![Page 4: CS 3370. Inserters and Extractors Stream State Files Streams String Streams Formatting Manipulators Internationalization.](https://reader035.fdocuments.in/reader035/viewer/2022062523/5a4d1adf7f8b9ab059976692/html5/thumbnails/4.jpg)
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;}
![Page 5: CS 3370. Inserters and Extractors Stream State Files Streams String Streams Formatting Manipulators Internationalization.](https://reader035.fdocuments.in/reader035/viewer/2022062523/5a4d1adf7f8b9ab059976692/html5/thumbnails/5.jpg)
istream& operator>>(istream& is, Date& d) { is >> d.month; char dash; is >> dash; if(dash != '-') { is.putback(dash); is.setstate(ios::failbit); // Input disabled return is; } is >> d.day; is >> dash; if(dash != '-') { is.putback(dash); is.setstate(ios::failbit); } is >> d.year; return is;}
![Page 6: CS 3370. Inserters and Extractors Stream State Files Streams String Streams Formatting Manipulators Internationalization.](https://reader035.fdocuments.in/reader035/viewer/2022062523/5a4d1adf7f8b9ab059976692/html5/thumbnails/6.jpg)
4 states: eof: set upon an attempt to read past end-of-file
▪ sets fail automatically▪ eof is meaningless for output streams
fail: an operation failed (e.g., alpha chars when reading int) bad: stream is broken (no memory for buffer, device failure) good: none of the other 3 states occurred
When an error occurs (fail or bad), the stream is disabled All subsequent stream operations are ignored Can re-enable stream operations with clear( )
![Page 7: CS 3370. Inserters and Extractors Stream State Files Streams String Streams Formatting Manipulators Internationalization.](https://reader035.fdocuments.in/reader035/viewer/2022062523/5a4d1adf7f8b9ab059976692/html5/thumbnails/7.jpg)
Can test with associated member functions: good( ), eof( ), fail( ), bad( )
Can test for successful input like this: if (strm) same as if (!strm.fail( ) && !
strm.bad() && ! strm.eof( ))
Can also use exceptions
![Page 8: CS 3370. Inserters and Extractors Stream State Files Streams String Streams Formatting Manipulators Internationalization.](https://reader035.fdocuments.in/reader035/viewer/2022062523/5a4d1adf7f8b9ab059976692/html5/thumbnails/8.jpg)
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 ios is a base class for streams
See strmexcept.cpp
![Page 9: CS 3370. Inserters and Extractors Stream State Files Streams String Streams Formatting Manipulators Internationalization.](https://reader035.fdocuments.in/reader035/viewer/2022062523/5a4d1adf7f8b9ab059976692/html5/thumbnails/9.jpg)
while (myStream >> x) // process x (this assumes no input failure)
while (getline(myStream, line)) // process line (ditto)
You can check conditions separately if (myStream.eof( )) … if (myStream.fail( )) …
![Page 10: CS 3370. Inserters and Extractors Stream State Files Streams String Streams Formatting Manipulators Internationalization.](https://reader035.fdocuments.in/reader035/viewer/2022062523/5a4d1adf7f8b9ab059976692/html5/thumbnails/10.jpg)
getgetline ignoreputbackungetpeek
![Page 11: CS 3370. Inserters and Extractors Stream State Files Streams String Streams Formatting Manipulators Internationalization.](https://reader035.fdocuments.in/reader035/viewer/2022062523/5a4d1adf7f8b9ab059976692/html5/thumbnails/11.jpg)
get( ) returns the next character, or -1 whitespace included
get(char& c) puts the character read in c returns the stream
get(char* s, int n, char delim = ‘\n’) reads n characters or up to delim
![Page 12: CS 3370. Inserters and Extractors Stream State Files Streams String Streams Formatting Manipulators Internationalization.](https://reader035.fdocuments.in/reader035/viewer/2022062523/5a4d1adf7f8b9ab059976692/html5/thumbnails/12.jpg)
getline(char* s, int n, char delim = ‘\n’) returns stream reads and discards delim (different from get)
std namespace scope version uses a string, not a char* declared in <string> getline(istream& is, string& s, char
delim = ‘\n’) returns stream
![Page 13: CS 3370. Inserters and Extractors Stream State Files Streams String Streams Formatting Manipulators Internationalization.](https://reader035.fdocuments.in/reader035/viewer/2022062523/5a4d1adf7f8b9ab059976692/html5/thumbnails/13.jpg)
Discards characters ignore(int n = 1, int delim =
eof( )) returns stream
For a large n, use: std::numeric_limits<streamsize>::max()
![Page 14: CS 3370. Inserters and Extractors Stream State Files Streams String Streams Formatting Manipulators Internationalization.](https://reader035.fdocuments.in/reader035/viewer/2022062523/5a4d1adf7f8b9ab059976692/html5/thumbnails/14.jpg)
unget() Moves the stream’s get pointer back one The next input op re-reads the previous
characterputback(char)
Puts an arbitrary character into the buffer So the next input op reads that character
peek() Returns the next character without moving
the get pointer beyond it
![Page 15: CS 3370. Inserters and Extractors Stream State Files Streams String Streams Formatting Manipulators Internationalization.](https://reader035.fdocuments.in/reader035/viewer/2022062523/5a4d1adf7f8b9ab059976692/html5/thumbnails/15.jpg)
Classes ifstream, ofstream, fstream declared in <fstream>
Constructors open, destructors close automatically
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 a bitwise-or ( | )
![Page 16: CS 3370. Inserters and Extractors Stream State Files Streams String Streams Formatting Manipulators Internationalization.](https://reader035.fdocuments.in/reader035/viewer/2022062523/5a4d1adf7f8b9ab059976692/html5/thumbnails/16.jpg)
Can move around in a stream except when using the console, of course
Using functions seekp( ), seekg( ) seekp( ) seeks in the output buffer (p =
“put”) seekg( ) seeks in the input buffer (g = “get”) Simultaneous I/O streams share the same
buffer▪ File streams keep the put/get pointers together▪ In string streams they’re independent
![Page 17: CS 3370. Inserters and Extractors Stream State Files Streams String Streams Formatting Manipulators Internationalization.](https://reader035.fdocuments.in/reader035/viewer/2022062523/5a4d1adf7f8b9ab059976692/html5/thumbnails/17.jpg)
Often used by databases can access records randomly
Fields must have only built-in data numbers, C-style strings, static arrays no pointers! uses binary mode
Use the write and read member functions
See employeedb.cpp
![Page 18: CS 3370. Inserters and Extractors Stream State Files Streams String Streams Formatting Manipulators Internationalization.](https://reader035.fdocuments.in/reader035/viewer/2022062523/5a4d1adf7f8b9ab059976692/html5/thumbnails/18.jpg)
Classes istringstream, ostringstream, stringstream declared in <sstream>
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: C04/IString.cpp,
C04/Ostring.cpp,C04/HTMLStripper2.cpp
![Page 19: CS 3370. Inserters and Extractors Stream State Files Streams String Streams Formatting Manipulators Internationalization.](https://reader035.fdocuments.in/reader035/viewer/2022062523/5a4d1adf7f8b9ab059976692/html5/thumbnails/19.jpg)
Can set stream attributes width, fill character, alignment, numeric
base, floating-point format, decimal precision, etc.
Use setf( ) and unsetf( )Example: C04/Format.cpp
![Page 20: CS 3370. Inserters and Extractors Stream State Files Streams String Streams Formatting Manipulators Internationalization.](https://reader035.fdocuments.in/reader035/viewer/2022062523/5a4d1adf7f8b9ab059976692/html5/thumbnails/20.jpg)
The data area(s) held by the stream One for input, one for output
▪ Streams that support both, have both Can access via the function rdbuf( )
A “Way Station” for data en routeUsually don’t worry about itOne cool feature:
C04/SType.cpp, hexdec.cpp
![Page 21: CS 3370. Inserters and Extractors Stream State Files Streams String Streams Formatting Manipulators Internationalization.](https://reader035.fdocuments.in/reader035/viewer/2022062523/5a4d1adf7f8b9ab059976692/html5/thumbnails/21.jpg)
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
![Page 22: CS 3370. Inserters and Extractors Stream State Files Streams String Streams Formatting Manipulators Internationalization.](https://reader035.fdocuments.in/reader035/viewer/2022062523/5a4d1adf7f8b9ab059976692/html5/thumbnails/22.jpg)
#include <iostream> Define a function with the required signature (below) 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’
![Page 23: CS 3370. Inserters and Extractors Stream State Files Streams String Streams Formatting Manipulators Internationalization.](https://reader035.fdocuments.in/reader035/viewer/2022062523/5a4d1adf7f8b9ab059976692/html5/thumbnails/23.jpg)
setw(n), setfill(c), setprecision(n), etc.
Must include <iomanip> for theseExample: C04/Manips.cppDifficult to implement your own
not portableUse Effectors instead
(see next slide)
![Page 24: CS 3370. Inserters and Extractors Stream State Files Streams String Streams Formatting Manipulators Internationalization.](https://reader035.fdocuments.in/reader035/viewer/2022062523/5a4d1adf7f8b9ab059976692/html5/thumbnails/24.jpg)
Create a class whose constructor formats a string according to its purpose
That class also provides an operator<<
Example: C04/Effector.cpp
![Page 25: CS 3370. Inserters and Extractors Stream State Files Streams String Streams Formatting Manipulators Internationalization.](https://reader035.fdocuments.in/reader035/viewer/2022062523/5a4d1adf7f8b9ab059976692/html5/thumbnails/25.jpg)
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 internally
![Page 26: CS 3370. Inserters and Extractors Stream State Files Streams String Streams Formatting Manipulators Internationalization.](https://reader035.fdocuments.in/reader035/viewer/2022062523/5a4d1adf7f8b9ab059976692/html5/thumbnails/26.jpg)
The template that governs the standard stream classes:template<class charT, class traits = char_traits<charT> >class basic_istream {...};
typedef basic_istream<char> istream;typedef basic_istream<wchar_t> wistream;typedef basic_ifstream<char> ifstream;typedef basic_ifstream<wchar_t> wifstream;typedef basic_istringstream<char> istringstream;typedef basic_istringstream<wchar_t> wistringstream;
![Page 27: CS 3370. Inserters and Extractors Stream State Files Streams String Streams Formatting Manipulators Internationalization.](https://reader035.fdocuments.in/reader035/viewer/2022062523/5a4d1adf7f8b9ab059976692/html5/thumbnails/27.jpg)
Cultural customization of I/O formatting
A stream has an associated localeCan change it with imbue( )Example: Locale.cpp (Windows only) Java’s locale support is much better