Main Page

Previous Page
Next Page

[Page 877 (continued)]

Answers to Self-Review Exercises


a) 1s, 0s. b) bit. c) file. d) characters. e) database. f) close. g) get. h) open. i) read. j) seekg, seekp.

  1. False. Function read can read from any input stream object derived from istream.

  2. False. These four streams are created automatically for the programmer. The <iostream> header must be included in a file to use them. This header includes declarations for each stream object.

  3. False. The files will be closed when destructors for ifstream, ofstream or fstream objects execute when the stream objects go out of scope or before program execution terminates, but it is a good programming practice to close all files explicitly with close once they are no longer needed.

  4. False. Member function seekp or seekg can be used to reposition the put or get fileposition pointer to the beginning of the file.

  5. [Page 878]
  6. True.

  7. False. In most cases, sequential file records are not of uniform length. Therefore, it is possible that updating a record will cause other data to be overwritten.

  8. True.

  9. False. Records in a random-access file normally are of uniform length.

  10. False. It is possible to seek from the beginning of the file, from the end of the file and from the current position in the file.

  1. ifstream inOldMaster( "oldmast.dat", ios::in );
  2. ifstream inTransaction( "trans.dat", ios::in );
  3. ofstream outNewMaster( "newmast.dat", ios::out);
  4. inOldMaster >> accountNumber >> name >> currentBalance;
  5. inTransaction >> accountNum >> dollarAmount;
  6. outNewMaster << accountNum << name << currentBalance;
  1. Error: The file payables.dat has not been opened before the attempt is made to output data to the stream.

    Correction: Use ostream function open to open payables.dat for output.

  2. Error: The incorrect istream object is being used to read a record from the file named payables.dat.

    Correction: Use istream object inPayable to refer to payables.dat.

  3. Error: The file's contents are discarded because the file is opened for output (ios::out).

    Correction: To add data to the file, open the file either for updating (ios::ate) or for appending (ios::app).

Previous Page
Next Page