Study Guide 14: Streams (File I/O)

  1. A C++ stream is
    1. The flow of control through a function.
    2. A flow of data from one place to another.
    3. The sequence of machine instructions that the CPU fetches, decodes, and executes.
    4. a file.
  2. Name the three stream classes commonly used for file I/O.
  3. Write a statement that creates an output file object named salefile and associates it with a file called SALES.JUN.
  4. Some streams work with input, and some with output.
    1. True
    2. False
  5. A file contains the following definitions:
    ostream   fileOut;
    char      ch;
    Write a statement that prints the character stored in ch, to fileOut.
  6. What operation could you use to write data of type float to a file accessed with an instance ofstream?
    1. the insertion operator
    2. seekg
    3. write
    4. put
  7. Define what the current position means when applied to files.
    1. The byte location at which the next read or write operation will take place.
    2. The position of the file on the hard drive.
    3. The position of the file within the file system.
    4. Both b and c.
  8. Write the prototype for the overloaded extractor operator (>>) that reads the contents of an instance of class Sample. Variable names are optional in prototypes.
  9. Given the following definition, choose the best way to read in a string that might contain spaces.
    char s[100];
    1. cin >> s;
    2. cin.getline(s, 100);
    3. getline(cin, s);
  10. Given the following definition, choose the best way to read in a string that might contain spaces.
    string s;
    1. cin >> s;
    2. cin.getline(s, 100);
    3. getline(cin, s);
  11. Why was binary mode added to C++ file I/O?
  12. On a Windows system, what is the difference between opening a file in text or binary mode?
  13. On a POSIX system, what is the difference between opening a file in text or binary mode?
  14. Choose the code to instantiate an input object and open a file named temp.txt for reading in text or ASCII mode:
    1. ios in(Ctemp.txt", ios::in);
    2. ifstream in("temp.txt", ios::binary | ios::in);
    3. ifstream in("temp.txt");
    4. iostream in("temp.txt", ios::in);
    5. fstream in("temp.txt");
  15. Choose the code to instantiate an output object and open a file named temp.dat for writing in binary mode:
    1. ofstream out("temp.dat", ios::binary);
    2. ofstream out("temp.dat");
    3. fstream out("temp.dat", ios::binary);
    4. iostream out("temp.dat", ios::binary);
    5. fstream out("temp.txt");
  16. Choose the code to instantiate an IO object and open a file named temp.txt for both reading and writing in text mode:
    1. iostream("temp.txt", ios::in | ios::out);
    2. ifstream("temp.txt", ios::in | ios::out);
    3. fstream file("temp.txt");
    4. fstream file("temp.txt", ios::in | ios::out);
  17. Choose the code to instantiate an IO object and open a file named temp for both reading and writing in binary mode:
    1. iostream("temp.txt", ios::binary | ios::in | ios::out);
    2. ifstream("temp.txt", ios::binary | ios::in | ios::out);
    3. fstream file("temp.txt"ios::binary);
    4. fstream file("temp.txt", ios::binary | ios::in | ios::out);
  18. What is the "current working directory?"
  19. When used as part of a file pathname, what does . represent
  20. When used as part of a file pathname, what does .. represent
  21. Choose all examples of absolute or full file names
    1. C:\Users\fred\util.cfg
    2. ..\Users\fred\util.cfg
    3. /Home/fred/util.cfg
    4. ../Home/fred/util.cfg
    5. fred/util.cfg
    6. /util.cfg
    7. \util.cfg
    8. util.cfg
  22. Choose all examples of relative file names
    1. /data.txt
    2. ./data.txt
    3. ../data.txt
    4. data.txt
    5. \data.txt
    6. .\data.txt
    7. ..\data.txt
    8. ../../util/data.txt
    9. ..\..\util/data.txt
  23. input is an open instance of ifstream and c is an int. Choose the code demonstrating how to read the complete file one byte at a time without extra loops. (The ellipses denote irrelevant code.)
    1. while ((c = input.get()) != EOF) { . . . }
    2. while (!input.eof()) { c = input.get(); . . . }
    3. Both a and b will work
    4. Neither a nor b will work
  24. Using C++ streams, write the statements to open a file named data.txt for reading in text mode and test if the file opened successfully.
  25. Using C++ streams, write the statements to open a file named data for writing in binary mode, without losing data already in the file and appending all new data to the end of the file.
  26. Using C++ streams, write a complete program to read from a file named input.txt and write to a file named output.txt. The program should: