Study Guide 14: Streams (File I/O) Answers

  1. b
  2. ofstream salefile("SALES.JUN");
  3. a
  4. Either of
  5. a or c. Let os and f be ostream and float variables, respectively:
  6. a
  7. friend istream& operator>> (istream&, Sample&);
  8. b
  9. c
  10. Computers store data in files as a sequence of bytes without meaning beyond their numerical value. Programs give deeper meaning to data by how they use it. C and C++ were developed on Unix, the original POSIX operating system. POSIX systems terminate the lines in text files with a newline character, which programs use to process text files by lines. However, Windows terminates the lines in text files with a two-character sequence: a carriage return and a newline or line feed. Consequently, programs written for one line-terminating protocol will fail when run on a system using the other protocol. So, the short answer is, "To make it easier to port or move programs between POSIX and Windows systems." The following two answers expand on how binary mode operates.
  11. In text mode, file read operations convert carriage return newline sequences to a single newline and write operations convert a newline to a carriage return newline sequence. Text mode, the default, allows programs that process files by lines to work on both Windows and POSIX systems without modification. However, some files (e.g., images, audio, or video) contain numeric data that is not organized by lines, making the conversions inappropriate. In binary mode, no conversions occur, leaving the data intact and unchanged.
  12. None. File I/O operations do not modify the data, leaving its interpretation to the processing program. POSIX systems accept but ignore binary mode when opening a file.
  13. c
  14. a
  15. c or d
  16. d
  17. The current working directory or CWD is the location in the file system that the operating system assigns to a program or process when running it. To the program, it "looks like" its position in the file system. The program uses this location for every relative file access it makes.
  18. The current working directory.
  19. The parent of, or one level up from, the current working directory.
  20. a, c, f, & g
  21. b, c, d, f, g, h, & i
  22. a
  23. ifstream in("data.txt");
    if (!in.good()) . . .
    	or
    if (in.fail()) . . .
    
  24. Either of
  25. #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main()
    {
        ifstream in("input.txt");
    
        if (!in.good())
        {
            cerr << "Unable to open " << input << endl;
            exit(1);
        }
    
        ofstream out("output.txt");
    
        if (!out.good())
        {
            cerr << "Unable to open " << output << endl;
            exit(1);
        }
    
        int c;
    
        while ((c = in.get()) != EOF)
            switch(c)
            {
                case 'a':
                    out << 'A';
                    break;
                default:
                    out << (char)c;
                    break;
            }
    
        // alternate
        /*while ((c = in.get()) != EOF)
            if (c == 'a')
                out << 'A';
            else
                out << (char)c;*/
    
        return 0;
    }
    The variable and while loop can also be written as:
    char c;
    while (in.get(c))