streams ii - web.stanford.edu · the std::cin stream is an example of an input stream. all input...
TRANSCRIPT
![Page 2: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/2.jpg)
Recap
Stream Miscellany
Stream Internals
Stream Manipulators
Stringstream
Tying it all Together
Game Plan
![Page 3: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/3.jpg)
Announcements
![Page 4: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/4.jpg)
Recap
![Page 5: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/5.jpg)
Output Streams
Can only receive data.
● The std::cout stream is an example of an output stream.● All output streams are of type std::ostream.
Send data using stream insertion operator: <<
Insertions converts data to string and sends to stream.
![Page 6: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/6.jpg)
Input Streams
Can only give you data.
● The std::cin stream is an example of an input stream.● All input streams are of type std::istream.
Pull out data using stream extraction operator: >>
Extraction gets data from stream as a string and converts it into the appropriate type.
![Page 7: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/7.jpg)
There are some quirks with extracting a string from a stream.
Reading into a string using >> will only read a single word, not the whole line.
To read a whole line, use
getline(istream& stream, string& line);
Reading Data From a File
![Page 8: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/8.jpg)
Some additional methods for using streams:
input.get(ch); // reads one char at a time
input.close(); // closes stream
input.clear(); // resets any fail bits
input.open("filename"); // open stream on file
input.seekg(0); // rewinds stream to start
Stream Miscellany
![Page 9: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/9.jpg)
Stream Internals
![Page 10: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/10.jpg)
Writing to a console/file is a slow operation.
If the program had to write each character immediately, runtime would significantly slow down.
What can we do?
Buffering
![Page 11: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/11.jpg)
Idea:
Accumulate characters in a temporary buffer/array.
When buffer is full, write out all contents of the buffer to the output device at once.
Buffering
This process is known as flushing the stream
![Page 12: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/12.jpg)
Let’s look at this in action:
Stream Buffering
(StreamBuffer.pro)
Buffering
![Page 13: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/13.jpg)
The internal sequence of data stored in a stream is called a buffer.
Istreams use them to store data we haven’t used yet
Ostreams use them to store data they haven’t passed along yet.
Buffering
![Page 14: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/14.jpg)
If we want to force the contents of the buffer to their destination, we can flush the stream:
stream.flush(ch); // use by default
stream << std::flush; // use if you are printing
flush(stream) // no good reason to use this
stream << std::endl; // use if you want a newline
Flushing the Buffer
![Page 15: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/15.jpg)
If we want to force the contents of the buffer to their destination, we can flush the stream:
stream.flush(ch); // use by default
stream << std::flush; // use if you are printing
flush(stream) // no good reason to use this
stream << std::endl; // use if you want a newline
Flushing the Buffer
![Page 16: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/16.jpg)
If we want to force the contents of the buffer to their destination, we can flush the stream:
stream.flush(ch); // use by default
stream << std::flush; // use if you are printing
stream << std::endl; // use if you want a newline
Flushing the Buffer
![Page 17: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/17.jpg)
If we want to force the contents of the buffer to their destination, we can flush the stream:
stream.flush(ch); // use by default
stream << std::flush; // use if you are printing
stream << std::endl; // use if you want a newline
Flushing the Buffer
This is equivalent to stream << "\n" << std::flush;
![Page 18: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/18.jpg)
Let’s look at this in action:
Stream Buffering
(StreamBuffer.pro)
Buffering
![Page 19: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/19.jpg)
Not all streams are buffered (std::cerr is an example).
We can get a very real sense of the speed difference:
Buffering
Stream Buffering Speed
(BufferSpeed.pro)
![Page 20: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/20.jpg)
![Page 21: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/21.jpg)
Streams have four bits to give us information about their state:
● Good bit No errors, the stream is good to go
● EOF bit End-of-file was reached during a previous operation
● Fail bit Logical error on a previous operation
● Bad bit Likely unrecoverable error on previous operation
Stream bits
![Page 22: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/22.jpg)
1. Read data2. Check if data is valid, if not break3. Use data4. Go back to step 1
while(true) { stream >> temp; if(stream.fail()) break; do_something(temp);}
Which bit to use?
![Page 23: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/23.jpg)
Stream Shortcuts
![Page 24: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/24.jpg)
The << and >> operators are not magic, they are actually functions!
Chaining >> or <<
std::cout << "hello";
operator<<(std::cout, "hello");
![Page 25: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/25.jpg)
We know functions can return things.
The << and >> operators return the stream passed as their left argument.
This is why this works:
Chaining >> or <<
std::cout << "hello" << 23 << "world";
![Page 26: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/26.jpg)
We know functions can return things.
The << and >> operators return the stream passed as their left argument.
This is why this works:
Chaining >> or <<
(((std::cout << "hello") << 23) << "world");
![Page 27: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/27.jpg)
We know functions can return things.
The << and >> operators return the stream passed as their left argument.
This is why this works:
Chaining >> or <<
(((std::cout << "hello") << 23) << "world");
![Page 28: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/28.jpg)
We know functions can return things.
The << and >> operators return the stream passed as their left argument.
This is why this works:
Chaining >> or <<
(((std::cout) << 23) << "world");
![Page 29: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/29.jpg)
We know functions can return things.
The << and >> operators return the stream passed as their left argument.
This is why this works:
Chaining >> or <<
((std::cout << 23) << "world");
![Page 30: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/30.jpg)
We know functions can return things.
The << and >> operators return the stream passed as their left argument.
This is why this works:
Chaining >> or <<
((std::cout << 23) << "world");
![Page 31: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/31.jpg)
We know functions can return things.
The << and >> operators return the stream passed as their left argument.
This is why this works:
Chaining >> or <<
((std::cout) << "world");
![Page 32: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/32.jpg)
We know functions can return things.
The << and >> operators return the stream passed as their left argument.
This is why this works:
Chaining >> or <<
(std::cout << "world");
![Page 33: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/33.jpg)
Let’s look at this code again:
Which bit to use? - Part II
while(true) { stream >> temp; if(stream.fail()) break; do_something(temp);}
![Page 34: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/34.jpg)
Let’s look at this code again:
Which bit to use? - Part II
while(true) { stream >> temp; if(stream.fail()) break; do_something(temp);}
Streams can be converted to bool
![Page 35: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/35.jpg)
Let’s look at this code again:
Which bit to use? - Part II
while(true) { stream >> temp; if(!stream) break; do_something(temp);}
Streams can be converted to bool
![Page 36: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/36.jpg)
Let’s look at this code again:
Which bit to use? - Part II
while(true) { stream >> temp; if(!stream) break; // checks fail or bad bit do_something(temp);}
![Page 37: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/37.jpg)
Let’s look at this code again:
Which bit to use? - Part II
while(true) { stream >> temp; if(!stream) break; // checks fail or bad bit do_something(temp);}
We know this returns the stream.
![Page 38: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/38.jpg)
Let’s look at this code again:
while(true) { if(!(stream >> temp)) break; do_something(temp);}
Which bit to use? - Part II
![Page 39: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/39.jpg)
Let’s look at this code again:
while(true) { if(!(stream >> temp)) break; do_something(temp);}
Which bit to use? - Part II
We can simplify the logic
![Page 40: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/40.jpg)
Let’s look at this code again:
while(stream >> temp) { do_something(temp);}
Which bit to use? - Part II
![Page 41: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/41.jpg)
The same principle applies with getline
while(stream >> temp) { do_something(temp);}
Which bit to use? - Part II
while(getline(stream, temp)) { do_something(temp);}
![Page 42: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/42.jpg)
Stream Manipulators
![Page 43: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/43.jpg)
There are some special keywords that change the behaviour of the stream when inserted.
std::endl and std::flush are two examples.
Stream Manipulator
![Page 44: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/44.jpg)
Common:● endl inserts a newline and flushes the stream● ws skips all whitespace until it finds another char● boolalpha prints “true” and “false” for bools
Numeric:● hex: prints numbers in hex● setprecision: adjusts the precision numbers print with
Padding:● setw pads output● setfill fills padding with character
Stream Manipulator
![Page 45: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/45.jpg)
Some examples - Padding
#include <iomanip>
std::cout << "[" << std::setw(10) << "Hi" << "]" << std::endl;
[ Hi]
Outputs:
![Page 46: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/46.jpg)
#include <iomanip>
std::cout << "[" << std::left << std::setw(10) << "Hi" << "]" << std::endl;
Some examples - Padding
[Hi ]
Outputs:
![Page 47: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/47.jpg)
#include <iomanip>
std::cout << "[" << std::left << std::setfill('-')<< std::setw(10) << "Hi" << "]" << std::endl;
Some examples - Padding
[Hi--------]
Outputs:
![Page 48: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/48.jpg)
#include <iomanip>
std::cout << std::hex << 10; // prints astd::cout << std::oct << 10; // prints 12std::cout << std::dec << 10; // prints 10
Some examples - Numeric
![Page 49: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/49.jpg)
Stream Manipulators - Recap
Stream manipulators can be passed into streams to change how they behave.
They have a variety of uses, and if you’d like to format something differently, there’s probably a manipulator for it.
You can find a list of the most common ones at http://www.cplusplus.com/reference/library/manipulators/
![Page 50: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/50.jpg)
stringstream
![Page 51: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/51.jpg)
Sometimes we want to be able to treat a string like a stream.
Useful scenarios:
● Converting between data types● Tokenizing a string
stringstream
![Page 52: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/52.jpg)
stringstream
#include <sstream>Std::string line = "137 2.718 Hello";std::stringstream stream(line);
int myInt;double myDouble;std::string myString;stream >> myInt >> myDouble >> myString;
std::cout << myInt << std::endl;std::cout << myDouble << std::endl;std::cout << myString << std::endl;
![Page 53: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/53.jpg)
Tying it Together
![Page 54: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/54.jpg)
Let’s write the Stanford simpio library!
Simple IO
(OurSimpIO.pro)
Buffering
![Page 55: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/55.jpg)
Next TimeSequential Containers
![Page 56: Streams II - web.stanford.edu · The std::cin stream is an example of an input stream. All input streams are of type std::istream. Pull out data using stream extraction operator:](https://reader036.vdocuments.net/reader036/viewer/2022081611/5f04cba87e708231d40fbf06/html5/thumbnails/56.jpg)