Topical Information

The purpose of this quiz is to give you a chance to focus your knowledge of basic file handling in C++.

Quiz Information

Questions

  1. Explain how, when your program works with a file, there are really two copies of the data of a file at any given time while your program's connected to the file. (Pictures might help clarify...)

    
        There is, of course, the copy of the file's data on the disk/drive where
        all of the data is stored together.  But there is also a copy of a part of
        the file's data -- a buffer full -- inside the stream object that we've
        connected to the physical file.  This buffer helps alleviate the speed
        difference between many physical devices and the CPU by bringing in chunks
        of the file at a time into main RAM for faster access.
    
    

  2. When reading from an input file, we can use the operator(s) and method(s) >>, peek, getline, and fail just as if we were reading from cin.

  3. To output to an output file, we use the _____ operator. We should also always remember to place _____ between every output item (or else it won't be readable later).

    1. ?:, * NO
    2. >>, endl NO
    3. +, spacing NO
    4. <<, spacing YES
    5. #, endl NO

  4.  
    TRUE   FALSE   Failure during a file open can be detected using either the fail method or the - operator on the file variable in question.
    TRUE   FALSE   We would normally use the good method to check for ANY failures during file processing.
    TRUE   FALSE   We could also explicitly use the eos and fail functions, but we might miss when something truly bad happens.
    TRUE   FALSE   Whichever way you go, you still should use a priming style loop or the end of file condition won't be handled properly.
  5. The symbols ios_base::nocreate and ios_base::noreplace unfortunately no longer appear in the C++ standard. The behavior upon opening an input file now won't create a file when that file doesn't already exist (on a standard-compliant compiler, that is). But, when opening an output file that already exists, the behavior is still defined to erase that file by default. To avoid such a circumstance, we must open the file for input to see if it does or doesn't exist and then (after closing that connection to it) open the file in either ios_base::out mode (the default) or ios_base::app mode (to add to the data already present). This decision is often left up to the user as it is, after all, their data.

  6. Show a skeleton of the basic end-of-file processing loop. Where do you read? Where do you process data? What condition(s) stop the loop? (Hint: There are 3 typical conditions that might go in the loop. Only one should be used at any given time for clarity.)

    
        Either:
    
            peeky-read
            while ( ! file.eof() )    // could also try !file.fail() or file.good()
            {
                read data
                process data
                peeky-read
            }
    
        Where 'peeky-read' is either a file.peek() for getline reads or a ws
        extraction for extraction reads.
    
        Or:
    
            while ( read data )
            {
                process data
            }
    
    

  7. Show the code to open an input file — ensuring that it opened properly. (Hint: What two things are normally forgotten during this loop?)

    
        prompt and read filename
        attempt to open file
        while ( ! file )
        {
            file.close()
            file.clear()
            error message, prompt, reread filename
            attempt to open file
        }
    
    

  8.  
    TRUE   FALSE   The name of a file is often coded into a literal string.
    TRUE   FALSE   When the user enters a file name we should use getline to allow their name to contain spacing.