public member function
<fstream>

std::fstream::fstream

default (1)
fstream();
initialization (2)
explicit fstream (const char* filename,                  ios_base::openmode mode = ios_base::in | ios_base::out);
default (1)
fstream();
initialization (2)
explicit fstream (const char* filename,                  ios_base::openmode mode = ios_base::in | ios_base::out);explicit fstream (const string& filename,                  ios_base::openmode mode = ios_base::in | ios_base::out);
copy (3)
fstream (const fstream&) = delete;
move (4)
fstream (fstream&& x);
Construct object and optionally open file
Constructs an fstream object:

(1) default constructor
Constructs an fstream object that is not associated with any file.
Internally, its iostream base constructor is passed a pointer to a newly constructed filebuf object (the internal file stream buffer).
(2) initialization constructor
Constructs a fstream object, initially associated with the file identified by its first argument (filename), open with the mode specified by mode.
Internally, its iostream base constructor is passed a pointer to a newly constructed filebuf object (the internal file stream buffer). Then, filebuf::open is called with filename and mode as arguments.
If the file cannot be opened, the stream's failbit flag is set.
(3) copy constructor (deleted)
Deleted (no copy constructor).
(4) move constructor
Acquires the contents of x.
First, the function move-constructs both its base iostream class from x and a filebuf object from x's internal filebuf object, and then associates them by calling member set_rdbuf.
x is left in an unspecified but valid state.
The internal filebuf object has at least the same duration as the fstream object.

Parameters

filename
A string representing the name of the file to be opened.
Specifics about its format and validity depend on the library implementation and running environment.
mode
Flags describing the requested input/output mode for the file.
This is an object of the bitmask member type openmode that consists of a combination of the following member constants:
member constantstands foraccess
ininputFile open for reading: the internal stream buffer supports input operations.
outoutputFile open for writing: the internal stream buffer supports output operations.
binarybinaryOperations are performed in binary mode rather than text.
ateat endThe output position starts at the end of the file.
appappendAll output operations happen at the end of the file, appending to its existing contents.
trunctruncateAny contents that existed in the file before it is open are discarded.
These flags can be combined with the bitwise OR operator (|).

If the mode has both trunc and app set, the opening operation fails. It also fails if either is set but out is not, or if both app and in are set.
If the mode has both trunc and app set, the opening operation fails. It also fails if trunc is set but out is not.
x
An fstream object, whose value is moved.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
// fstream constructor.
#include <fstream>      // std::fstream

int main () {

  std::fstream fs ("test.txt", std::fstream::in | std::fstream::out);

  // i/o operations here

  fs.close();

  return 0;
}

Data races

The move constructor (4) modifies x.

Exception safety

-

See also