CS304: Lecture 13 - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

CS304: Lecture 13

Description:

... stream operations, including cin, cout, cerr, and clog objects. ... ostream clog: Also connected to the standard error device, but is buffered. Stream Output ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 25
Provided by: matthe49
Category:
Tags: clog | cs304 | lecture

less

Transcript and Presenter's Notes

Title: CS304: Lecture 13


1
CS304 Lecture 13
  • Stream Input/Output
  • Deitel, Ch. 15
  • http//www.cis.uab.edu/cs304

2
Overview
  • The C I/O libraries are huge, providing an
    amazing amount of capabilities to the programmer.
  • Some features have (obviously) already been
    discussed (or else we wouldnt be doing I/O).
  • This chapter goes into more depth, but does not
    cover the library in its entirety.

3
The Libraries
  • Cs I/O libraries make use of many of Cs
    important features
  • Object-orientation
  • Function overloading
  • Operator overloading
  • Highly extensible
  • C I/O enforces type safety
  • Only relevant operations are used
  • Using an operation on a type for which it isnt
    defined generates a compiler error

4
Streams
  • I/O for C is dealt with in entities known as
    streams, or a sequence of bytes
  • Bytes flow from the streams (Eh? Eh?), or flow
    into streams, depending on the operations
    performed
  • I/O in general (not with just streams) is
    monumentally slow compared to processor speeds,
    so I/O should be used with care

5
Types of I/O
  • C allows both high- and low-level I/O, where
    low-level is interpreted as merely a sequence of
    bytes.
  • High level is formatted into meaningful types,
    based on what the user is expecting. This is
    generally preferred over low-level for obvious
    reasons.

6
Classic Streams vs. Standard Streams
  • Classic stream libraries used chars for input and
    output
  • Recall, chars are only 8-bits. This can cause
    problems for other character sets
  • Standard stream libraries are capable of
    supporting Unicode characters (using the
    character type wchar_t) in an overloaded library
  • This is now a non-issue, but could be important
    in older compilers.

7
iostream Library Headers
  • ltiostreamgt declares basic services needed for all
    stream operations, including cin, cout, cerr, and
    clog objects.
  • ltiomanipgt allow formatting of I/O with
    parameterized stream manipulators, e.g. setw and
    setprecision.
  • ltfstreamgt includes services for performing file
    I/O, which (luckily!) works a lot like cin and
    cout.

8
Stream Architecture
  • Streams follow a basic, inheritance-based
    architecture
  • This is described in fig 15.2 in the book, which
    I will now reproduce on the board.

9
Stream I/O Classes and Objects
  • istream cin connected to the standard input
    device, which can be the keyboard or a file, etc.
  • ostream cout Used with standard output, usually
    the screen or a file
  • ostream cerr Used with the standard error
    device (usually the screen). This is an
    unbufferred mechanism
  • ostream clog Also connected to the standard
    error device, but is buffered

10
Stream Output
  • Formatted and unformatted capabilities are
    provided by ostream
  • Finer points Output of a char
  • (void ), weve seen this before.
  • Using put to output a single character
  • cout.put(x)
  • These return the stream, so calls can be cascaded

11
Stream Input
  • Formatted and unformatted capabilities are
    offered by istream
  • gtgt usually skips white space (\t\n\r ) in the
    stream. (punt)
  • gtgt also returns a reference to the stream, so
    calls may cascade
  • This may also be used in a condition (e.g.
    while), where the operator magically returns
    success or failure instead of a reference

12
The more complicated answer
  • The void is overloaded, and is implicitly
    invoked to convert the reference into a non-null
    pointer if successful, or null if unsuccessful.
  • This information is kept in state bits for each
    stream (that also controls things such as
    formatting and errors). A failbit is set for
    wrong datatype, and a badbit is set for failed
    operations.

13
get
  • get takes a character off the stream (the inverse
    of put)
  • char x cin.get() // can use an optional
    delimiter
  • EOF is a character constant that can be tested
    for.
  • while ( ( char x cin.get() ) ! EOF )
  • Another possibility is using cin.eof(), which
    returns a boolean.
  • get can also take a specified number of chars off
    the stream and a char delimiter. These result in
    a null-terminated string.
  • cin.get(buffer, size) cin.get(buffer, size,
    delim)
  • Notice get will not ignore whitespace! And the
    delimiter is not removed from the stream!

14
getline
  • getline is similar to get (version 3). The
    delimiter is discarded.

15
Putback, Peek, Ignore
  • cin.putback(char) puts a character back on the
    stream.
  • cin.peek() returns the next character on the
    stream, but doesnt remove it
  • cin.ignore(int num1, char delimEOF) ignores the
    next num characters on the stream until the
    delimiter is reached.

16
Unformatted I/O
  • Use this to input and output raw bytes
  • cout.write(buffer, size) outputs from a char
    array
  • cin.read(buffer, size) inputs into a char array
  • If EOF is reached before size bytes are read, the
    failbit is set
  • gcount reports the number of characters read by
    the last input operation.

17
Stream Manipulators
  • Found in ltiomanipgt, these perform formatting
    tasks like setting field width, precision, and
    other format states, etc. etc.
  • These work by setting flags and fields within the
    object, which are always checked during output
  • These flags generally stay set until explicitly
    changed

18
dec, oct, hex, setbase
  • Examples speak volumes!
  • cin gtgt number
  • cout ltlt Hex ltlt hex ltlt number ltlt endl
  • cout ltlt Dec ltlt dec ltlt number ltlt endl
  • cout ltlt Oct ltlt oct ltlt number ltlt endl
  • cout ltlt Reset ltlt setbase(10) ltlt number ltlt
    endl

19
Precision
  • double root2 sqrt(2.0)
  • oldprecision cout.precision()
  • cout ltlt fixed
  • for (int places 0 places lt 9 places)
    cout.precision(places)cout ltlt root2 ltlt endl
  • for (int places 0 places lt 9 places)
    cout ltlt setprecision(places) ltlt root2 ltlt endl

20
Output
  • 1
  • 1.4
  • 1.41
  • 1.414
  • 1.4142
  • 1.41421
  • 1.414214 //Notice the rounding?
  • 1.4142136
  • 1.41421356
  • 1.414213562

21
Width, Setw
  • width(int) Sets the number of character
    positions used for output
  • Fill characters are inserted to pad out the field
    to length
  • To use a stream manipulator, use setw(int)
  • Usable on both input and output streams.

22
Trailing Zeros, Decimal Points
  • showpoint and noshowpoint controls whether the
    decimal point is shown and
  • If it is, it will fill to precision, i.e. the
    precision designated by setprecision(int) will be
    honored.
  • cout ltlt showpoint ltlt 1.0 ltlt endl
  • cout ltlt noshowpoint ltlt 1.0 ltlt endl

23
And many more!
  • I have probably run out of time. Please go
    through chapter 15 and have a good idea of what
    each manipulator does!
  • These are very easy, its just remembering all of
    them.

24
Stream Error States
  • Eof End of file on the stream
  • Fail format error, but recoverable
  • Bad General failure that results in loss of
    data
  • Good The operation succeeded
  • Rdstate Returns the error state of the stream
  • Clear Restore to good state after recovery
Write a Comment
User Comments (0)
About PowerShow.com