|
|
#include <fstream>void fstream::open(const char *filename, ios::openmode mode = ios::in | ios:: out); void ifstream::open(const char *filename, ios::openmode mode = ios::in); void ofstream::open(const char *filename, ios::openmode mode = ios:: out | ios::trunc);
The open( ) function is a member of fstream, ifstream, and ofstream.
A file is associated with a stream by using the open( ) function. Here, filename is the name of the file, which may include a path specifier. The value of mode determines how the file is opened. It must be one (or more) of these values:
ios::app
ios::ate
ios::binary
ios::in
ios::out
ios::trunc
You can combine two or more of these values by ORing them together.
Including ios::app causes all output to that file to be appended to the end. This value can be used only with files capable of output. Including ios::ate causes a seek to the end of the file to occur when the file is opened. Although ios::ate causes a seek to the end of file, I/O operations can still occur anywhere within the file.
The ios::binary value causes the file to be opened for binary I/O operations. By default, files are opened in text mode.
The ios::in value specifies that the file is capable of input. The ios::out value specifies that the file is capable of output. However, creating an ifstream stream implies input, creating an ofstream stream implies output, and opening a file using fstream implies both input and output.
The ios::trunc value causes the contents of a preexisting file by the same name to be destroyed and the file is truncated to zero length.
In all cases, if open( ) fails, the stream will be false. Therefore, before using a file, you should test to make sure that the open operation succeeded.
Related functions are close( ), fstream( ), ifstream( ), and ofstream( ).
|
|