streams - stanford university · output streams can only receive data. the std::cout stream is an...

38
Streams Ali Malik [email protected]

Upload: others

Post on 17-Oct-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 2: Streams - Stanford University · 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

Recap

Purpose of Streams

Output Streams

Input Streams

Stringstream (maybe)

Game Plan

Page 3: Streams - Stanford University · 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

Announcements

Page 4: Streams - Stanford University · 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

Recap

Page 5: Streams - Stanford University · 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

Recap - Hello, world!

#include <iostream>

int main() { std::cout << "Hello, world!" << std::endl; return 0;}

Page 6: Streams - Stanford University · 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

Recap - Hello, world!

#include <iostream>

int main() { std::cout << "Hello, world!" << std::endl; return 0;}

These can get annoying to write for common names like cout, endl, string etc.

Page 7: Streams - Stanford University · 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

Recap - Hello, world!

#include <iostream>

using std::cout;using std::endl;

int main() { std::cout << "Hello, world!" << std::endl; return 0;}

Page 8: Streams - Stanford University · 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

Recap - Hello, world!

#include <iostream>

using std::cout;using std::endl;

int main() { std::cout << "Hello, world!" << std::endl; return 0;}

Whenever you use cout, the compiler will assume you mean std::cout

Page 9: Streams - Stanford University · 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

Recap - Hello, world!

#include <iostream>

using std::cout;using std::endl;

int main() { std::cout << "Hello, world!" << std::endl; return 0;}

Whenever you use cout, the compiler will assume you mean std::cout

Page 10: Streams - Stanford University · 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

Recap - Hello, world!

#include <iostream>

using std::cout;using std::endl;

int main() { cout << "Hello, world!" << endl; return 0;}

Whenever you use cout, the compiler will assume you mean std::cout

Page 11: Streams - Stanford University · 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

Recap - Hello, world!

#include <iostream>

using std::cout;using std::endl;

int main() { cout << "Hello, world!" << endl; return 0;}

Whenever you use cout, the compiler will assume you mean std::cout

The using namespace std directive is a bazooka version of this.

Page 12: Streams - Stanford University · 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

Streams

Page 13: Streams - Stanford University · 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

"Designing and implementing a general input/output facility for a programming language is notoriously difficult"

- Bjarne Stroustrup

Page 14: Streams - Stanford University · 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

A stream is an abstraction for input/output.

You can think of it as a source (input) or destination (output) of characters of indefinite length.

Streams - Introduction

Page 15: Streams - Stanford University · 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

A stream is an abstraction for input/output.

You can think of it as a source (input) or destination (output) of characters of indefinite length.

Streams - Introduction

std::cout

<< "Hello, world!"

Page 16: Streams - Stanford University · 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

A stream is an abstraction for input/output.

You can think of it as a source (input) or destination (output) of characters of indefinite length.

Streams - Introduction

"Hello, world!"

std::cout

Page 17: Streams - Stanford University · 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

A stream is an abstraction for input/output.

You can think of it as a source (input) or destination (output) of characters of indefinite length.

Streams - Introduction

Hello, world!

std::cout

Page 18: Streams - Stanford University · 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

The Idea Behind Streams

Page 19: Streams - Stanford University · 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

You can write data of multiple types to stream objects.

The Idea Behind Streams

In particular, any primitive type can be inserted into a stream! For other types, you need to explicitly tell C++ how to do this.

cout << "Strings work!" << endl;cout << 1729 << endl;cout << 3.14 << endl;cout << "Mixed types - " << 1123 << endl;

Page 20: Streams - Stanford University · 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

How does this work?

Idea:

● Input from user is in text form (string)● Output to user is in text form (string)● Intermediate computation needs to be done on object type

The Idea Behind Streams

Page 21: Streams - Stanford University · 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

Streams allow a C++ programmer to convert between the string representation of data, and the data itself.

The Idea Behind Streams

Page 22: Streams - Stanford University · 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

Types of Streams

Page 23: Streams - Stanford University · 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

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 24: Streams - Stanford University · 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

We can use a std::ostream for more than just printing to a console.

You can send the data to a file using a std::ofstream, which is a special type of std::ostream.

Output Streams

Output Stream Example

(output.cpp)

Page 25: Streams - Stanford University · 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

Input Streams

Quick test!

How familiar is this:

int x;std::cin >> x;

Page 26: Streams - Stanford University · 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

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 27: Streams - Stanford University · 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

Just like with std::ostream, we can use a std::istream for more than just console IO.

You can read data from a file using a std::ifstream.

Input Streams

Input Stream Example

(input.cpp)

Page 28: Streams - Stanford University · 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

To understand a std::istream , think of it as a sequence of characters.

Input Streams

4 2 1 3 4 \n

position

Page 29: Streams - Stanford University · 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

Extracting an integer will read as many characters as possible from the stream.

Input Streams

4 2 1 3 4 \n

position

// input is an istreamint value;input >> value; // value is now 42

Page 30: Streams - Stanford University · 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

Extracting again will skip over any whitespace when reading the next integer.

Input Streams

4 2 1 3 4 \n

position

// input is an istreamint value;input >> value; // value is now 134

Page 31: Streams - Stanford University · 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

When no more data is left, the fail bit will be set to true and input.fail() will return true.

Input Streams

4 2 1 3 4 \n

position

// input is an istreamint value;input >> value; // value is now ??

Page 32: Streams - Stanford University · 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

Input Streams

More Input Stream Examples

(input.cpp)

Page 33: Streams - Stanford University · 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

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 34: Streams - Stanford University · 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

Input Streams

More Input Stream Examples

(input.cpp)

Page 35: Streams - Stanford University · 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

Think carefully when mixing >> and getline!

Using >> can have some weird bugs so next lesson we will talk about a way to avoid it by using getline and string streams.

Input Stream

Page 36: Streams - Stanford University · 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

What happens if you read into the wrong type?

Can you extract user defined types (e.g. classes) from a stream?

Can you control how output stream output the data we give them?

Is there a stream that might be both an input and output stream?

Some Questions to Ponder

Find out next time!

Page 37: Streams - Stanford University · 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

Next TimeStreams - The Details

Page 38: Streams - Stanford University · 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