Stream Manipulators

  • Upload
    sarath

  • View
    228

  • Download
    0

Embed Size (px)

Citation preview

  • 7/26/2019 Stream Manipulators

    1/28

    2000 Prentice Hall, Inc. All rights reserved.

    Outline21.6 Stream Manipulators21.6.1 Integral Stream Base:dec,oct,hexandsetbase21.6.2 Floating-Point Precision (precision,setprecision)21.6.3 Field Width (setw,width)21.6.4 User-Defined Manipulators21.7 Stream Format States21.7.1 Format State Flags21.7.2 Trailing Zeros and Decimal Points (ios::showpoint)21.7.3 Justification (ios::left,ios::right,ios::internal)21.7.4 Padding (fill,setfill)21.7.5 Integral Stream Base (ios::dec,ios::oct,ios::hex,ios::showbase)

    21.7.6 Floating-Point Numbers; Scientific Notation(ios::scientific,ios::fixed)21.7.7 Uppercase/Lowercase Control (ios::uppercase)21.7.8 Setting and Resetting the Format Flags (flags,setiosflags,

    resetiosflags)21.8 Stream Error States21.9 Tying an Output Stream to an Input Stream

    C++ Stream Input/Output Stream

    Manipulatrs

  • 7/26/2019 Stream Manipulators

    2/28

    2000 Prentice Hall, Inc. All rights reserved.

    21.6 Stream Manipulators

    ! Stream manipulatr capa"ilities# Setting $ield %idths

    # Setting precisins

    # Setting and unsetting $rmat $lags

    # Setting the $ill character in $ields

    # &lushing streams

    # Inserting a ne%line in the utput stream and $lushing the stream

    # Inserting a null character in the utput stream and s'ipping

    %hitespace in the input stream

  • 7/26/2019 Stream Manipulators

    3/28

    2000 Prentice Hall, Inc. All rights reserved.

    21.6.1 Integral Stream Base:dec, oct,hex andsetbase

    oct,hex,rdec(# Change "ase $ %hich integers are interpreted $rm the stream.

    )*ample(

    int n = 15;

    cout

  • 7/26/2019 Stream Manipulators

    4/28

    2000 Prentice Hall, Inc. All rights reserved.

    Outline

    1. Load header

    1.1 Initialize variables

    2. Input number

    3. Output in hex

    3.1 Output in octal

    3.2 Output in decimal

    1 %% Fig& '1&1: fig'1(1&cpp

    ' %% )sing hex, oct, dec and setbase stream manipulators&

    * +include

    5 usingstd::cout;

    usingstd::cin;

    - usingstd::endl;

    !

    . +include

    1

    11 usingstd::hex;

    1' usingstd::dec;

    1* usingstd::oct;

    1 usingstd::setbase;

    15

    1 intmain#$

    1- /

    1! intn;

    1.

    ' cout

  • 7/26/2019 Stream Manipulators

    5/28

    2000 Prentice Hall, Inc. All rights reserved.

    Outline

    Program Output

    0nter a decimal number: '' in hexadecimal is: 1' in octal is: '' in decimal is: '

  • 7/26/2019 Stream Manipulators

    6/28

    2000 Prentice Hall, Inc. All rights reserved.

    21.6.2 Floating-Point Precision#precision, setprecision$

    precision# Mem"er $unctin

    # Sets num"er $ digits t the right $ decimal pint

    cout&precision#'$;

    cout&precision#$ returns current precisin setting setprecision

    # Parameteried stream manipulatr

    # i'e all parameteried stream manipulatrs,reuired

    # Speci$1 precisin(cout

  • 7/26/2019 Stream Manipulators

    7/282000 Prentice Hall, Inc. All rights reserved.

    21.6.3 Field Width#setw,width$

    ios widthmem"er $unctin# Sets $ield %idth num"er $ character psitins a value shuld "e

    utput r num"er $ characters that shuld "e input-

    # eturns previus %idth

    # I$ values prcessed are smaller than %idth, $ill characters inserted

    as padding

    # cin&width#5$;

    setwstream manipulatrcin >> setw#5$ >> string;

    ! emem"er t reserve ne space $r the null

    character

  • 7/26/2019 Stream Manipulators

    8/282000 Prentice Hall, Inc. All rights reserved.

    Outline

    1. Initialize variables

    2. Input sentence

    2.1 Set width

    2.2 Loop and change

    width

    3. Output

    1 %% fig'1(1!&cpp

    ' %% 4emonstrating the width member function

    * +include

    5 usingstd::cout;

    usingstd::cin;

    - usingstd::endl;

    !

    . intmain#$

    1 /

    11 intw = ;

    1' charstring 1 6;

    1*

    1 cout > string $ /

    1! cout&width# w77 $;

    1. cout

  • 7/26/2019 Stream Manipulators

    9/282000 Prentice Hall, Inc. All rights reserved.

    Outline

    Program Output

    0nter a sentence:8his is a test of the width member function8his is a test

    of the widt h memb er func tion

  • 7/26/2019 Stream Manipulators

    10/282000 Prentice Hall, Inc. All rights reserved.

    21.7 Stream Format States

    ! &rmat $lags# Speci$1 $rmatting t "e per$rmed during stream I/O peratins

    setf, unsetfand flags# Mem"er $unctins that cntrl the $lag settings

  • 7/26/2019 Stream Manipulators

    11/282000 Prentice Hall, Inc. All rights reserved.

    21.7.1 Format State Flags

    ! &rmat State &lags# 3e$ined as an enumeratin in class ios

    # Can "e cntrlled "1 mem"er $unctins

    flags 9speci$ies a value representing the settings $ all the

    $lags! eturns longvalue cntaining prir ptins

    setf ne argument, 4rs4 $lags %ith e*isting $lags

    unsetf 9unsets $lags

    setiosflags parameteried stream manipulatr used t set

    $lags resetiosflags parameteried stream manipulatr, has same

    $unctins as unsetf

    ! &lags can "e cm"ined using "it%ise O -

  • 7/26/2019 Stream Manipulators

    12/282000 Prentice Hall, Inc. All rights reserved.

    21.7.2 Trailing Zeros and Decimal Points(ios::showpoint)

    ios::showpoint# &rces a $lat %ith an integer value t "e printed %ith its decimal

    pint and trailing ers

    cout&setf#ios::showpoint$

    cout

  • 7/26/2019 Stream Manipulators

    13/282000 Prentice Hall, Inc. All rights reserved.

    21.7.3 Justification (ios::left,ios::right, ios::internal)

    ios::left# &ields t le$t6usti$ied %ith padding characters t the right

    ios::right# 3e$ault setting# &ields right6usti$ied %ith padding characters t the le$t

    ! Character used $r padding set "1 fillmem"er $unctin

    setfillparameteried stream manipulatr

    # 3e$ault character is space

    %%

  • 7/26/2019 Stream Manipulators

    14/282000 Prentice Hall, Inc. All rights reserved.

    Outline

    1. Initialize variable

    2. Use parameterizedstream manipulators

    3. Output

    1 %% Fig& '1&'': fig'1(''&cpp

    ' %% eft9ustification and right9ustification&

    * +include

    5 usingstd::cout;

    usingstd::endl;

    -

    ! +include.

    1 usingstd::ios;

    11 usingstd::setw;

    1' usingstd::setiosflags;

    1* usingstd::resetiosflags;

    1

    15 intmain#$

    1 /1- intx = 1'*5;

    1!

    1. cout

  • 7/26/2019 Stream Manipulators

    15/282000 Prentice Hall, Inc. All rights reserved.

    Outline

    Program Output

    4efault is right ustified: 1'*5)?@ A0AB0C F)?D8E?)se setf to set ios::left:1'*5

    )se unsetf to restore default: 1'*5)?@ GCGA080CH04 8C0GA AG?)G8EC)se setiosflags to set ios::left:1'*5)se resetiosflags to restore default: 1'*5

  • 7/26/2019 Stream Manipulators

    16/282000 Prentice Hall, Inc. All rights reserved.

    21.7.4 Padding (fill, setfill)

    fill

    mem"er $unctin# Speci$ies the $ill character

    # Space is de$ault

    # eturns the prir padding character

    cout&fill# I$;

    setfillmanipulatr# Als sets $ill character

    cout

  • 7/26/2019 Stream Manipulators

    17/282000 Prentice Hall, Inc. All rights reserved.

    Outline

    1. Load header

    1.1 Initialize variable

    1 %% Fig& '1&': fig'1('&cpp

    ' %% )sing the fill member function and the setfill

    * %% manipulator to change the padding character for

    %% fields larger than the Jalues being printed&

    5 +include

    - usingstd::cout;

    ! usingstd::endl;

    .

    1 +include

    11

    1' usingstd::ios;

    1* usingstd::setw;

    1 usingstd::hex;

    15 usingstd::dec;

    1 usingstd::setfill;

    1-

    1! intmain#$

    1. /

    ' intx = 1;

    '1

  • 7/26/2019 Stream Manipulators

    18/282000 Prentice Hall, Inc. All rights reserved.

    Outline

    2. Set fill character

    3. Output

    Program Output

    '1

    '' cout

  • 7/26/2019 Stream Manipulators

    19/282000 Prentice Hall, Inc. All rights reserved.

    21.7.5- Integral Stream Base (ios::dec,ios::oct, ios::hex, ios::showbase)

    ios::basefieldstatic mem"er# 7sed similarl1 t ios::adustfield%ith setf

    # Includes the ios::oct, ios::hexand ios::dec$lag "its

    # Speci$1 that integers are t "e treated as ctal, he*adecimal and

    decimal values# 3e$ault is decimal

    # 3e$ault $r stream e*tractins depends n $rm inputted

    ! Integers starting %ith are treated as ctal

    ! Integers starting %ith xr Mare treated as he*adecimal# Once a "ase speci$ied, settings sta1 until changed

  • 7/26/2019 Stream Manipulators

    20/282000 Prentice Hall, Inc. All rights reserved.

    21.7.6 Floating-Point Numbers; ScientificNotation (ios::scientific,ios::fixed)

    ios::scientific# &rces utput $ a $lating pint num"er in scienti$ic

    ntatin(

    1&.e7.

    ios::fixed# &rces $lating pint num"ers t displa1 a speci$ic

    num"er $ digits t the right $ the decimal speci$ied%ithprecision-

  • 7/26/2019 Stream Manipulators

    21/282000 Prentice Hall, Inc. All rights reserved.

    21.7.6 Floating-Point Numbers; ScientificNotation (II)

    staticdata mem"er ios::floatfield# Cntains ios::scientificand ios::fixed

    # 7sed similarl1 t ios::adustfieldandios::basefieldin setf cout&setf#ios::scientific,ios::floatfield$;

    cout&setf#, ios::floatfield$restresde$ault $rmat $r utputting $latingpint num"ers

    1 %% Fig '1 ': fig'1 ' cpp

  • 7/26/2019 Stream Manipulators

    22/282000 Prentice Hall, Inc. All rights reserved.

    Outline

    1. Initialize variables

    2. Set flags

    3. Output

    Program Output

    1 %% Fig& '1&': fig'1('&cpp

    ' %% 4isplaNing floating9point Jalues in sNstem default,

    * %% scientific, and fixed formats&

    +include

    5

    usingstd::cout;

    - usingstd::endl;

    ! usingstd::ios;

    .1 intmain#$

    11 /

    1' doublex = &1'*5-, N = 1&.e.;

    1*

    1 cout

  • 7/26/2019 Stream Manipulators

    23/28

    2000 Prentice Hall, Inc. All rights reserved.

    21.7.7 Uppercase/Lowercase Control(ios::uppercase)

    ios::uppercase# &rces uppercase 0t "e utput %ith scienti$ic ntatin

    &*'071

    # &rces uppercase Mt "e utput %ith he*adecimal num"ers, and

    causes all letters t "e uppercase-5B40

  • 7/26/2019 Stream Manipulators

    24/28

    2000 Prentice Hall, Inc. All rights reserved.

    21.8 Stream Error States

    eofbit# Set $r an input stream a$ter end$$ile encuntered cin&eof#$returns truei$ end$$ile has "een encuntered ncin

    failbit# Set $r a stream %hen a $rmat errr ccurs

    cin&fail#$ returns truei$ a stream peratin has $ailed

    #5rmall1 pssi"le t recver $rm these errrs

  • 7/26/2019 Stream Manipulators

    25/28

    2000 Prentice Hall, Inc. All rights reserved.

    21.8 Stream Error States (II)

    badbit# Set %hen an errr ccurs that results in data lss cin&bad#$returns truei$ stream peratin $ailed

    # nrmall1 nnrecvera"le

    goodbit# Set $r a stream i$ neither eofbit, failbitrbadbitare set cin&good#$returns truei$ thebad, failand eof$unctins

    %uld all return $alse.# I/O peratins shuld nl1 "e per$rmed n 8gd9 streams

    rdstate# eturns the state $ the stream

    # Stream can "e tested %ith a switchstatement that e*amines all$ the state "its

    # )asier t use eof,bad, fail, and goodt determine state

  • 7/26/2019 Stream Manipulators

    26/28

    2000 Prentice Hall, Inc. All rights reserved.

    21.8 Stream Error States (III)

    clear# 7sed t restre a stream:s state t 8gd9

    cin&clear#$ clears cinand sets goodbit$r the stream

    cin&clear# ios::failbit $ actuall1sets the failbit

    ! Might d this %hen encuntering a pr"lem %ith a user

    de$ined t1pe

    ! Other peratrs operatorO

    ! eturns truei$badbitr failbitset

    operator JoidI! eturns falsei$badbitr failbitset

    # 7se$ul $r $ile prcessing

    1 %% Fig& '1&'.: fig'1 '.&cpp

  • 7/26/2019 Stream Manipulators

    27/28

    2000 Prentice Hall, Inc. All rights reserved.

    Outline

    1. Initialize variable

    2. Function calls

    3. Output

    %% g g ( pp

    ' %% 8esting error states&

    * +include

    5 usingstd::cout;

    usingstd::endl;

    - usingstd::cin;

    !. intmain#$

    1 /

    11 intx;

    1' cout

  • 7/26/2019 Stream Manipulators

    28/28

    Outline

    Program Output

    Before a bad input operation:cin&rdstate#$: cin&eof#$: cin&fail#$: cin&bad#$: cin&good#$: 1

    0xpects an integer, but enter a character: GGfter a bad input operation:cin&rdstate#$: ' cin&eof#$: cin&fail#$: 1 cin&bad#$: cin&good#$:

    Gfter cin&clear#$cin&fail#$: cin&good#$: 1

    http://var/www/apps/conversion/tmp/scratch_6/