Streams - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Streams

Description:

Prints someString immediately. clog someString; Buffered. Prints someString only when output buffer is full or flushed. 9. Priority of Stream Operators ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 18
Provided by: dwightbarn
Category:
Tags: prints | streams

less

Transcript and Presenter's Notes

Title: Streams


1
Streams
Chapter 8
2
C Input/Output Streams
  • The basic data type for I/O in C is the stream
  • We've used the standard input/output streams
  • istream cin built-in input stream variable
  • ostream cout built-in output stream variable
  • The I/O functions of C can be also be used in a
    C program
  • From library ltstdiogt scanf (input), printf
    (output)
  • In CS-201, use streams for input and output of
    data

3
iostream Library Header Files
  • iostream library
  • ltiostreamgt cin, cout, cerr, and clog objects
  • ltiomanipgt parameterized stream manipulators
  • ltfstreamgt information important to
    user-controlled file processing operations

4
Output the Insertion Operator
  • The standard I/O streams are defined in the
    iostream library. Accessing the objects and
    functions from this library requires the
    following pre-processor directive include
    ltiostreamgt
  • Then output is achieved with a statement like
    cout ltlt " X " ltlt X
  • The ltlt (insertion operator) is
  • Binary, left associative
  • Requires an output stream (like cout) for its
    left argument
  • Supports many data type for its right argument
  • Returns an output stream object
  • Thus Insertions to an output stream can be
    "chained" together

5
Tracing Output Execution
  • Assume X 25, A 10, B -3
  • cout ltlt " X " ltlt X //"X " is output
  • cout ltlt X //"25" is output
  • cout //no effect
  • cout ltlt (A B) ltlt " " ltlt (A B) ltlt endl
  • cout ltlt 7 ltlt " " ltlt (A B) ltlt endl
  • cout ltlt 7 ltlt " " ltlt 13 ltlt endl
  • cout ltlt " " ltlt 13 ltlt endl
  • cout ltlt 13 ltlt endl
  • cout ltlt endl
  • cout

6
Input the Extraction Operator
  • As with the standard output stream, cout, to use
    the standard input stream (cin) a program must
    use the pre-compiler directive include
    ltiostreamgt
  • Then input is achieved with a statement like
    cin gtgt X where X is the name of the variable
    assigned the value read from the keyboard

7
Input the Extraction Operator
  • gtgt (stream-extraction) used to perform stream
    input
  • Normally ignores whitespaces (spaces, tabs,
    newlines)
  • Returns zero (false) when EOF is encountered
  • otherwise returns a reference to the object from
    which it was invoked (i.e. cin)
  • This enables cascaded input. cin gtgt x gtgt y

8
iostream Stream Objects
  • istream input streams
  • cin gtgt someVariable
  • cin knows what type of data is to be assigned to
    someVariable (based on the type of someVariable)
  • ostream output streams
  • cout ltlt someVariable
  • cout knows the type of data to output
  • cerr ltlt someString
  • Unbuffered. Prints someString immediately
  • clog ltlt someString
  • Buffered. Prints someString only when output
    buffer is full or flushed

9
Priority of Stream Operators
  • gtgt and ltlt have relatively high precedence
  • conditional assignment expressions must be in
    parenthesescout ltlt "1 2 " ltlt (x 5)
  • NOTcout ltlt "1 2 " ltlt x 5

10
Character output with put
  • put member function
  • Outputs one character to specified stream
  • cout.put('A')
  • May be called with an ASCII-valued expression
  • cout.put(65)
  • outputs A

11
get Member Functions
  • cin.get() inputs a character from stream (even
    white spaces) and returns it
  • cin.get(c) inputs a character from stream and
    stores it in c
  • Reference parameter

12
Type-Safe I/O
  • ltlt and gtgt operators
  • Designed to accept data of different types
  • When unexpected data encountered, error flags set
  • Program stays in control

13
Extraction Operator and Whitespace
  • In programming, common characters that do not
    produce a visible image on a page or in a file
    are referred to as whitespace
  • The most common whitespace characters are
  • newline (\n)
  • tab (\t)
  • space
  • By default, the extraction operator in C will
    ignore leading whitespace characters
  • That is, the extraction operator will remove
    leading whitespace characters from the input
    stream and discard them
  • A mechanism exists for overiding this behavior

14
Details of an Extraction
Assume the input stream cin contains 12 17.3
-19
  • Suppose that X is declared as an int, and the
    following statement is executed cin gtgt X
  • The type of X determines how the extraction is
    performed
  • First, any leading whitespace characters are
    discarded
  • Since an integer value is being read, the
    extraction will stop when a character that
    couldn't be part of an integer is found
  • The digits '1' and '2' are extracted, and the
    next character is whitespace, so the extraction
    stops and X gets the value 12
  • The whitespace after the '2' is left in the input
    stream

15
ignore() Member Function
  • There is also a way to remove and discard
    characters from an input stream cin.ignore(5,
    '')
  • read and discard characters from the input stream
    until
  • 5 characters have been discardedor
  • A '' character has been read and discarded

16
Stream Manipulators Member Functions
  • stream manipulator/member function capabilities
  • setting field widths
  • setting precisions
  • setting the format of bool output
  • setting the fill character in fields
  • flushing streams
  • inserting a newline in the output stream and
    flushing
  • inserting a null character in the output stream
  • skipping whitespace in the input stream

17
Some examples of cout member functions
  • precision member function
  • sets number of digits to the right of decimal
    point
  • cout.precision(2)
  • cout.precision() returns current precision
    setting, unchanged
  • setf member function
  • Used to set one of many formats
  • Often used in place of manipulators
  • cout.setf(ios_baseboolalpha)
Write a Comment
User Comments (0)
About PowerShow.com