Topical Information

The purpose of this question set is to give you a chance to focus your knowledge of processing streams/files in C++.

Quiz Information

Questions

  1. Explain the use of the putback method. Why do we normally not use this method? What method do we use instead?

    
        The idea of the putback method is to place the given char into the
        most recently read spot in the input stream.
    
        This is a terrible idea as we shouldn't be modifying an input stream
        at all and we don't.  We modify the buffer version of it only.  This
        can lead to confusion down the line and is best avoided entirely.
    
        To get around using putback, we use peek to look ahead at the next
        to be read spot in the input stream and decide before reading it what
        to do with it.  (putback, you see, is for making decisions after we
        read data and putting it back if it isn't what we wanted to see just
        then.)
    
    

  2. To position an input file stream to a certain position (measured in _____), you can use the _____ method. By default, position offsets are relative to the beginning of the file (specified by _____), but you can also specify that the offset is relative to the file's current reading position (with _____) or relative to the end of the file (with _____).

    1. characters, tellg, ios_base::start, ios_base::here, ios_base::stop NO
    2. feet, position, ios_base::beginning, ios_base::current, ios_base::ending NO
    3. bytes, seekg, ios_base::beg, ios_base::cur, ios_base::end YES
    4. lines, putback, ios_base::first, ios_base::middle, ios_base::last NO
    5. data items, peek, ios_base::start, ios_base::current, ios_base::end NO

  3.  
    TRUE   FALSE   The only position which is valid in any file is 0.
    TRUE   FALSE   To figure out another valid position, you must use the seekg method of the stream.
    TRUE   FALSE   When positioning relative to the end of the file, remember that the end of file marker counts as position 0 there.
    TRUE   FALSE   Negative position offsets are often used relative to the beginning and end of the file.
  4. The _____ method will remove the next character from the stream and return it for the programmer's use. The _____ method, on the other hand, will remove the next character from the stream and throw it away. Finally, the _____ method will return a copy of the next character from the stream to the programmer without removing it from the stream.

    1. seekg, tellg, putback NO
    2. peek, ignore, new_line NO
    3. get, getline, put NO
    4. get, ignore, peek YES
    5. gimme, throw_out, look_see NO

  5. When passing a stream to a function, it must always be passed as a reference. Also, thanks to inheritance, we can pass both the keyboard stream (cin) or an input file stream through the compatible type istream. Similarly, you can use the compatible type ostream to pass both output files and the screen stream (cout) to a function. The only time you must pass a file stream by its natural type (ifstream or ofstream) is if you are going to use methods such as open or close which cannot be applied to the console streams.

  6. Given that a file contains the English alphabet on a single line (nothing more; not even a newline), show what happens during the following code. (Assume that the statements are executed sequentially as shown.)

        file.seekg(0);
        cout << static_cast<char>(file.get());
        file.seekg(12,ios_base::cur);
        cout << static_cast<char>(file.peek());
        file.seekg(-4,ios_base::cur);
        cout << static_cast<char>(file.get());
        file.ignore();
        cout << static_cast<char>(file.get());
        file.seekg(10);
        cout << static_cast<char>(file.get());
        file.seekg(-4,ios_base::end);
        cout << static_cast<char>(file.peek());
        file.seekg(-1,ios_base::end);
        cout << static_cast<char>(file.get());
        cout << file.tellg();
        cout << static_cast<char>(file.get());
        file.seekg(0);
        cout << static_cast<char>(file.get());
    
    
        First a little guide to help us track where we are:
    
                  1         2
       +012345678901234567890123456
        ABCDEFGHIJKLMNOPQRSTUVWXYZ
       -654321098765432109876543210
              2         1
    
        And now the answer:
    
            ANJLKWZ26?A
    
        Notes:
    
        The ? represents some garbage representation of an EOF marker which isn't
        a proper char and so is not going to print right under that typecast.
    
        The last A assumes this library implementation is up to the latest because
        seekg now does a clear before moving.  Otherwise we'd be stuck on the EOF
        marker in an older library.
    
    

  7. Code a function to remove all characters from a stream (specified in the argument list) until an end-of-line is encountered. (Thought-provoker: Should the end-of-line character be removed as well?)

    
        Although this would do it:
    
            inline void remove_all(istream & instream)
            {
                instream.ignore(numeric_limits<streamsize>::max(), '\n');
                return;
            }
    
        This forces the newline to be removed with the rest.
    
        I'd also accept the more manual:
    
            inline void remove_all(istream & instream, bool leave_newline = false)
            {
                while ( instream.peek() != '\n' )
                {
                    instream.ignore();
                }
                if ( ! leave_newline )
                {
                    instream.ignore();
                }
                return;
            }
    
        This has the advantage of allowing the caller to decide the answer to the
        newline question.  Such flexibility makes the function more reusable in
        more situations.
    
    

  8. Explain how you can allow the user to place comments at the top of their data file. If you need to re-process the data from the file, what two things should you do to make things as effective as possible for your user.

    
        First, pick a comment marker.  One character is easiest to detect as we can
        use peek to find it.  Next peek for the next character and compare to the
        comment marker.  If it's the same, ignore that whole line and repeat.  If
        it isn't, stop and record the read position with tellg.
    
        Later, when you need to reprocess the file's contents, do a seekg to the
        start of the data to avoid all the comments in one move instead of another
        loop of compare/ignore tedium.
    
    

  9.  
    TRUE   FALSE   ios_base::failbit is set when the stream finds disk problems or looses its connection to the disk file.
    TRUE   FALSE   ios_base::badbit is set when a requested extraction is poorly formed (letters where a number was expected).
    TRUE   FALSE   ios_base::eofbit is set when you have read up to the end of the file.
    TRUE   FALSE   The clear method can be used to reset all of these error flags to a good state.
    TRUE   FALSE   The clear method can also be useful when closing and reopening a stream because the standard isn't clear on the state of error flags upon closing.