Streams What is a stream a sequence of

  • Slides: 13
Download presentation
Streams • What is a stream? § a sequence of characters used for program

Streams • What is a stream? § a sequence of characters used for program input or output § the newline character (‘n’) partitions a stream into a sequence of lines § User-defined types istream and ostream are defined by classes in the iostream library § cin and cout are objects of these types Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1

Streams • Standard input/output streams § include iostream header file § cin (connected to

Streams • Standard input/output streams § include iostream header file § cin (connected to keyboard) § reads characters typed at keyboard § cout (connected to your display) § displays characters to your screen § can also use a stream in a condition Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2

Input • During input § Stream operators must convert sequence of characters into their

Input • During input § Stream operators must convert sequence of characters into their appropriate internal representations § Store them in memory § A stream has no fixed size Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3

Input • For the character sequence 123. 45 • read in as a type

Input • For the character sequence 123. 45 • read in as a type float variable, the stream extraction operator >> must convert this to the floating point form of 123. 45 for storage • read in as a type int variable, only the first three characters will be extracted and converted into the binary equivalent of 123 for storage • read in as a type char variable, only the first character (‘ 1’) is extracted and stored Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4

Input Conversion • Characters to internal representation Copyright © 2007 Pearson Education, Inc. Publishing

Input Conversion • Characters to internal representation Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5

Output • Output character sequence 123. 45 – When writing a floating point number

Output • Output character sequence 123. 45 – When writing a floating point number to the output stream, the insertion operator << must convert its internal representation to a sequence of characters – Write this character sequence to the output stream Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6

Output Conversion • From internal representation to characters Copyright © 2007 Pearson Education, Inc.

Output Conversion • From internal representation to characters Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7

Input/Output Conversion • A stream has no fixed size • When inserting information to

Input/Output Conversion • A stream has no fixed size • When inserting information to a stream, add characters to the end of the stream; the stream output function keeps track of the stream size • When extracting information from a stream, the input stream buffer pointer keeps track of the last character read • Refer to Example 8. 2 (pp 446) Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8

Using a Stream in a Condition • Alternative to using the eof function to

Using a Stream in a Condition • Alternative to using the eof function to test for the end of a stream • You can test the status of a stream by using the stream name directly in a condition • Since an input operation on stream cin returns a reference to stream cin, we can test the status of stream cin • The condition value is true if the input operation was successful; false if the input operation was unsuccessful Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9

Example cin. get(next); while (!cin. eof()) { while (next != 'n') { cout. put(next);

Example cin. get(next); while (!cin. eof()) { while (next != 'n') { cout. put(next); … cin. get(next); } cout. put('n'); cin. get(next); } // get first character of new line // check for end of file // check for end of line // output character // get next character // mark end of current display line // get next character Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10

Read from File Example #include <cstdlib> // definition of EXIT_FAILURE #include <fstream> // for

Read from File Example #include <cstdlib> // definition of EXIT_FAILURE #include <fstream> // for external file streams #include <iostream> // for cin and cout … #define in. File "In. Data. txt“ // associate name of stream with // name of external file Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 11

Read from File Example int main() { ifstream ins; // ins is an input

Read from File Example int main() { ifstream ins; // ins is an input stream … ins. open( in. File ); // connects ins to file in. File if ( ins. fail () ) { cerr << "*** ERROR: Cannot open " << in. File << " for input. " << endl; return EXIT_FAILURE; // failure return } // end if … Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12

Read from File Example (continue) while ( ins >> a. String ) { cout

Read from File Example (continue) while ( ins >> a. String ) { cout << a. String << ' '; } // end while ins. close(); // close input file stream } // end main() Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13