Presentation is loading. Please wait.

Presentation is loading. Please wait.

Streams One of the themes of this course is that everything can be reduced to simple (and similiar) concepts. Streams are one example. Keyboard and Screen.

Similar presentations


Presentation on theme: "Streams One of the themes of this course is that everything can be reduced to simple (and similiar) concepts. Streams are one example. Keyboard and Screen."— Presentation transcript:

1 Streams One of the themes of this course is that everything can be reduced to simple (and similiar) concepts. Streams are one example. Keyboard and Screen are the standard I/O devices. Later, when we need to add file I/O, we will see that files are also streams. Everything we just learned will be the same for files as it was for standard I/O.

2 C++ Stream I/O  C++ introduces the concept of a stream as the input/output object. (Java also uses the stream concept)  A stream contains characters coming from some input device  Or a stream contains characters going to some output device

3 C++ Stream I/O (cont) To use standard stream I/O, we include the header file iostream.h in our program To use file I/O, we include the header file fstream.h in our program. #include To use a character array as a stream, we include the header file strstrea.h in our program.

4 Input and Output (Standard) Input uses an istream Output uses an ostream cin is associated with the standard input device, i.e. the keyboard cout is associated with the standard output device, i.e. the monitor screen

5 Input and Output ( files ) Input uses an ifstream Output uses an ofstream inData is then associated with the input file outData is associated with the the output file #include ifstream inData; ofstream outData;

6 Extraction and Insertion To get characters from the input stream, we use the extraction operator >>. To put characters on the output stream, we use the insertion operator <<. The direction the operator points, makes it easy to remember which is which.

7 Extraction and Insertion (cont) cin >> someInt means extract (get) some integer from the input stream. cout << someInt means insert (put) some integer on the output stream

8 Streams Streams convert characters into values of appropriate data types. cin >> someInt looks for a sequence of characters which represents an integer. cout << "The answer is " << someInt first outputs a string and then outputs the characters representing the value of someInt.

9 Streams (cont) cin and cout operate on character, or text streams. Text streams have the concept of lines and '\n' is the newline character. cout << someInt << '\n';

10 Streams (cont) endl is a better way to represent the newline character since it is more in keeping with the concepts of abstraction. cout << someInt << endl;

11 get and getline Sometimes we want more control over the input or output stream. cin.get(ch) will get a single character. cin.getline(str,81) will get an entire line (up to 80 characters) and will consume the newline character. cin.ignore(80, '\n') skips 80 characters or skip to the start of the next line.

12 put and putback cout.put(ch) outputs a single character. cin.putback(ch) returns a character to the input stream.

13 Testing the state of a stream When processsing characters from the input stream, we often need to know when the stream is empty of characters. The stream returns a value for the success or failure of the last operation. cin >> someInt; // the priming read while (cin) {// the test for success process(someInt); // do loop if true cin >> someInt; // the looping read }


Download ppt "Streams One of the themes of this course is that everything can be reduced to simple (and similiar) concepts. Streams are one example. Keyboard and Screen."

Similar presentations


Ads by Google