Lecture Notes for Introduction to Computer Science II

24 July 2001 - Formatted I-O


  1. formatted vs. unformatted i-o

    1. unformatted i-o doesn't change the data being read or written

    2. formatted i-o changes the data being read or written

    3. why change - 3 is not the same as '3' or "3"

      1. humans understand with characters; machines understand with bits

      2. characters go to and come from humans

      3. but characters are bits - but bits of the wrong kind

      4. 3 = 0000000000000011 and '3' = 0000001100000011 = 51

      5. for humans to understand 3, it has to be output as '3' (or 51)

    4. how change - convert numbers to characters and vice versa

      1. the number 100 has to be changed into the characters '1', '0', and '0'

      2. the characters '5', '2', and '1' have to be converted into the number 512

    5. formatted i-o converts between numbers and characters in both directions

      1. not only numbers can be converted

      2. characters - not much of a conversion

      3. strings - ditto

      4. booleans

      5. even objects

  2. stream extraction (input) and insertion (output)

    1. the stream insertion operator outs << value

      1. outs is an io stream opened for writing

      2. value is a value of a type the insertion operator knows how to convert (int, char, string and so on)

      3. value is converted into characters

      4. the characters are appended to the stream at the file pointer

      5. the file pointer moves to the byte just after the last byte written

    2. the stream extraction operator ins >> var

      1. extraction is not as simple as insertion

      2. ins is an io stream opened for reading

      3. var is a variable having a type the extraction operator knows how to convert

      4. starting at the file pointer, extraction reads as many bytes as there are consistent with the type of value being read

        1. for integers - the characters '0' through '9' possibly proceeded by either '-' or '+'; all the digits are read, but not necessarily used

        2. for characters - the next bytes, single quotes not needed

        3. the file pointer points to the next byte after the last byte read

        4. the characters are converted to the proper value and assigned to var

      5. whitespace and stream extraction

        1. in general, stream extraction does not consider white-space characters as being part of any value, including strings

        2. white-space characters include space, tab, and newline

        3. initial whitespace is skipped

        4. trailing whitespace ends extraction

      6. incorrect input

        1. if the input characters don't match the expected value, the extraction fails

        2. the file pointer does not advance past the bad character - white space stays skipped over

        3. var does not receive a value

        4. good() returns false

    3. extraction and insertion return the stream involved

      1. cascading insertions and extractions - cout << "i = " << i << "\n"

      2. if cin hits eof, it returns NULL - while (cin >> i)


This page last modified on 23 July 2001.