Streams - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Streams

Description:

C introduces the concept of a stream as the input/output object. ... Extraction and Insertion ... on the output stream, we use the insertion operator ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 14
Provided by: bob4217
Category:

less

Transcript and Presenter's Notes

Title: Streams


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 ltiostream.hgt include ltfstream.hgt
  • 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 )

include ltfstream.hgt ifstream inData ofstream
outData
  • Input uses an ifstream
  • Output uses an ofstream
  • inData is then associated with the input file
  • outData is associated with the the output file

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

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

8
  • Streams
  • Streams convert characters into values of
    appropriate data types.
  • cin gtgt someInt looks for a sequence of characters
    which represents an integer.
  • cout ltlt "The answer is " ltlt 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 ltlt someInt ltlt '\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 ltlt someInt ltlt 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 gtgt someInt // the priming
read while (cin) // the test for success
process(someInt) // do loop if true
cin gtgt someInt // the looping
read
Write a Comment
User Comments (0)
About PowerShow.com