fstream ( );
explicit fstream ( const char * filename,
ios_base::openmode mode = ios_base::in | ios_base::out );
Construct object and optionally open file
Constructs an object of the fstream class. This implies the initialization of the associated filebuf object and the call to the constructor of its base class with the filebuf object as parameter.
Additionally, when the second constructor version is used, the stream is associated with a physical file as if a call to the member function open with the same parameters was made.
If the constructor is not successful in opening the file, the object is still created although no file is associated to the stream buffer and the stream's failbit is set (which can be checked with inherited member fail).
Parameters
- filename
- C-string containing the name of the file to be opened.
- mode
- Flags describing the requested i/o mode for the file. This is an object of type ios_base::openmode, which consists on a combination of one or more of the following flags defined as member constants:
flag value | opening mode |
app | (append) Set the stream's position indicator to the end of the stream before each output operation. |
ate | (at end) Set the stream's position indicator to the end of the stream on opening. |
binary | (binary) Consider stream as binary rather than text. |
in | (input) Allow input operations on the stream. |
out | (output) Allow output operations on the stream. |
trunc | (truncate) Any current content is discarded, assuming a length of zero on opening. |
Return Value
none
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
// using fstream constructors.
#include <iostream>
#include <fstream>
using namespace std;
int main () {
fstream filestr ("test.txt", fstream::in | fstream::out);
// >> i/o operations here <<
filestr.close();
return 0;
}
|
Basic template member declaration
( basic_fstream<charT,traits> )
1 2
|
basic_fstream();
explicit basic_fstream ( const char * filename, ios_base::openmode mode = ios_base::in | ios_base::out);
|
See also
|