因为是继承的关系所以东西差不多,但是有一些特殊的成员,以下列出

#include <fstream>
fstream fstrm;   // have constructors, usage is same as open member
fstrm.open(s); fstrm.open(s, mode);
fstrm.close();
fstrm.is_open(); // bool, indicate whether the file was successfully opened and has not been closed.

The open and close Members

Indeed, calling open on a file stream that is already open will fail and set failbit. Subsequent attempts to use that file stream will fail.

When an fstream object is destroyed, close is called automatically.

File Modes

  • in
  • out
  • app: append. Seek to the end before every write
  • trunc: Truncate the file. May be set only when out is also specified
  • ate: Seek to the end immediately after the open.
  • binary:

ate and binary are specified in combination with any other file modes.

Any time open is called, the file mode is set, either explicitly or implicitly.

Using an fstream in Place of an iostream&

因为有继承的关系,所以 functions that are written to take a reference (or pointer) to one of the iostream types can be called on behalf of the corresponding fstream (or sstream) type.