Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Streams.

Similar presentations


Presentation on theme: "Data Streams."— Presentation transcript:

1 Data Streams

2 Extraction Extraction Operator [ cin>> num1]
Extracts formatted data from an input stream Data in the same format as the data type that it is being extracted to. Ex. extracting to an integer will attempt to take whatever is in the stream and conform it to the integer data type. Ignores whitespace

3 Extraction Questions

4 Extraction Questions How do we extract whitespace?
What happens if the data in the stream doesn’t match the data type? What if there is junk in the stream and we need to get rid of it?

5 Extraction How do we extract whitespace? Unformatted Extraction
Character Extraction The get function – extracts a single character from the stream. String Extraction The getline function – extracts a string from the stream. It needs a delimiter (or just use the default)

6 Extraction What happens if the data in the stream doesn’t match the data type? Stream failure! No additional information can be extracted from the stream The status of a stream can be checked using two functions. The good function The fail function The stream can be reset back to the good state by using the clear function.

7 Extraction What if there is junk in the stream and we need to get rid of it? We can clear the stream of all the data in the stream (up to a certain number of characters or up to a delimiter) by using the ignore function.

8 Insertion Insertion Operator [ cout << num;]
Inserts data into an output stream We can immediately send all the data in the stream to the output device using the flush function.

9 Keyboard Monitor Program.exe cin cout Stream Diagram 1.0

10 Streams Questions

11 Streams Questions What other devices can we connect to a program using a stream? How do we write to or read from a file?

12 Stream Diagram 2.0 Program.exe Keyboard Monitor cin cout File.in
File.out Stream Diagram 2.0

13 Stream Diagram 2.0 Program.exe Keyboard Monitor cin cout File.in
File.out Stream Diagram 2.0

14 File Streams Include the header file for file streams
#include <fstream> Declaration of a input file stream ifstream fin; // you can use any variable name Declaration of a output file stream ofstream fout; // you can use any variable name Is that it? Can we use these file streams now?

15 File Streams Is that it? Can we use these file streams now?
No! We need to establish a connection between the file and the program. By using the open function we associate a file stream with a file. fin.open ( “file.in” ); Remember to disassociate a stream with a file after you are done with it. fin.close ( );

16 Stream Diagram 2.0 Program.exe Keyboard Monitor cin cout File.in
File.out fout fin Stream Diagram 2.0

17 File Streams Questions

18 File Streams Questions How do we use these streams now?


Download ppt "Data Streams."

Similar presentations


Ads by Google