Skip to main contentCal State San Bernardino / [CNS] / [Comp Sci Dept] / [R J Botting] >> [CSci202] >> 10
[Index] [Schedule] [Syllabi] [Text] [Labs] [Projects] [Resources] [Search] [Contact] [Grading]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Thu May 3 13:45:36 PDT 2007

Contents


    CSci202 Computer Science II, Session 10 Input/Output Streams


      (previous): Exceptions [ 09.html ]

      Preparation

      Study this page and section 11.1 thru to 11.5 (and stop there) and write down the questions, doubts, and surprises that you have on a piece of paper.

      Assigned Work Due

      Hand in your questions, doubts and surprises. Inlcude a page number and name.

      Project 3 (Ch 9) due in.

      Input

        Review formatted input/output

          Note the book works top-down, from the general to the particular. You may need to read sections 11.1 to 11.5 twice.

          Bookmark tables in book for lab work. List functions on your cheat sheet for quizzes and finals.

          Look out for small typing errors in some class names on pages 368-369. Correct them in your notes/copy.

          Here is some sample code [ t10.cpp ] (do we have the libraries named?) [ 10.cpp ] (check spelling of libraries and class names).

          11.1 the class ios

          Things I wish we didn't have to know about.

          On UNIX/Linux use Ctrl-D to terminate input.

          11.2 Reading streams

          1. 11.2.1 Formatted Input
          2. 11.2.2 Unformatted input
          3. Fast example: copy file from cin to cout, each character on one line: [ chars.cpp ] (note... this also uses the helpful < cctype > library [ c++.cctype.html ] )

          11.3 Output to streams

          1. 11.3.1 Formatted output
          2. 11.3.2 Unformatted output

          11.4 Connecting files to streams

          1. rdbuf?? [ rdbuf.cpp ]

        11.5 Arguments to main

        Start with a nice simple example [ Echo.cpp ] which is very like the example on page 392.

        In the example code on page 393 there should be 9 spaces in the initial value of blank

         	char blank[] = "         ";
        and the next statement is a neat trick to make the number of blanks the right length.

        Here is another simple example of argv and fstream: [ Cat.cpp ] which is used like this

         		./Cat nip.txt
        to output the contents of a file called nip.txt. Or this
         		./Cat Cat.cpp
        which lists the program source code on the terminal...

      Questions

        What is class ios

        It is a convenient place for the C++ standard library to store a dozen or more special constants that are used to control Input and Output of Streams.

        You will see these turn up as things like

         		ios:app
        (signals opening a file for appending new data after the existing data).

        What is the difference between formatted and unformatted input?

        Unformatted input is copied, one character to one byte, from the input source into a piece of memory -- normally an array of characters.

        Formatted input takes in characters and parses them, figures out what data they represent, and puts the result in a variable or object of a particular type. The translation depends on the type of the variable or object. There are predefined input for all predefined data types: ints, floats, etc. Many different codings are allowed as well.

        Consequence: formatted input is slower, more powerful, more likely to fail, and most often used.

        Can you input data without knowing its format?

        This takes extra effort. First read in unformatted data into a string or character arrray. Then use if statements and strstreams/stringstreams [ 11.html ] to try out different formats till one fits.

        In the worst case scenario you end up writing a small compiler...

        Are all readable streams from class istream?

        No. fstreams can be read and written. istream is for streams that can not be written!

        Is ios and abstract class?

        I don't think so because it seems to have objects like ios.in. On the other hand... I don't think that most programmers ever have to declare a variable of type ios.

        Will we be needing #includes other than <iostream>?

        Yes.

        What are manipulators?

        A manipulator is an object that appears together with either a << or a >>. In other words these operators are defined for it. They change the behavior of the stream. This is high power magic... but safe when used as described on the label. Side-effects: your code gets shorter.

        Do you feel like being a magician and experiemnting with iomainpulators? If so see [ Do-it-yourself Manipulators in iomanip ] which shows you how to define some simple ones.

        Why does assert(x==y) always fail?

        This happens most often when x and y are floating point expressions. NEVER use == and != to compare floating or double numbers. Rounding errors make the result false.

        Use this instead:

         		fabs(x-y) < the_acceptable_error

        This is also true of using == and != in an if or a while.

        What happens if you don't close a stream?

        The end of the program should invoke the destructors for all open streams and these should close the files.

        What do cin and cout stand for

        "C input" and "C output" respectively.

        Notice that "i" and "o" are used consistently to indicate "input" and "output".

        What is cerr?

        It is a stream like cout that is for reporting error messages to a user running the program on a terminal.

        What is clog?

        It is a stream like cout that is for recording what a program has done: a log.

        I've never found a use for it, yet.

        Can you give an example of how to connect a file to a stream?

        Here [ fq.cc ] is an old example. You should try it and make it work with a .cpp extension etc. There will be some more examples TBA. Including Lab06.

        What happens if nothing catches a thrown exception?

        The program terminates. A message like "Aborted", "ABEND", etc. is output. Typically the user gets angry and smashes something.

        What does setprecision(n) do?

        It controls how many deimals are displayed. I don't memorise the details. They won't be needed in quizes or the final. BUT you need a good reference to look up the details when you need to use it in a practice.

        Notice: it does not change how values are calculated. Calculations are done in double length in C++. It just controls what the user of the program sees.

        Where is iomanip used on page 384?

        setw(...)

        Is there an online listing of input output commands in C++?

        Yes. See [ iomanip.html ] (trancribed from the GCC library) and [ lib-iostreams.html ] (from the draft C++ standard [ http://csci.csusb.edu/dick/c++std/cd2/ ] ).

        How do you change data inside a file

        This can get very messy unless the file is deliberately set up to make it easy. The file is like a book and replacing a word in the book by a longer word is usually impossible....

        But if the file consists of a long series of records that are all the same length, you can use "seekg" to jump to the record to change and read it into primary memory, then edit the data in memory, seekp back to the start of the record and put the updated data back into the record.

        Can C++ create images?

        Yes.... but it is only easy if you include libraries that have the write functions in them. There are a dozen separate librarys for doing imges in C++. None are a true standard, so I don't cover them or ask questions about them on the final/quizzes.

        Can C++ access networked files?

        C++ relies on the operating system to make remote files available to it. The same commands and streams then do the work.

      Exercises

      Depends on the questions!

    . . . . . . . . . ( end of section CSci202 Computer Science II, Session 10 Input/Output Streams) <<Contents | End>>

    Quizzes to come

    Note the material in this class will be tested in a a quiz in class 12.

    Lab on exceptions

    [ lab05.html ]

    Next

    Topic (chapter 11.6, 11.8, and 11.9). Introduction to Binary files and strings [ 12.html ]

    Abreviations

  1. TBA::="To Be Announced", something I have to do.
  2. TBD::="To Be Done", something you have to do.
  3. Dia::="A free Open Source Diagramming tool for Linux, Windoze, etc. ".

End