public member function
<fstream>

std::basic_ifstream::open

void open (const char* filename,  ios_base::openmode mode = ios_base::in);
void open (const   char* filename,  ios_base::openmode mode = ios_base::in);void open (const string& filename,  ios_base::openmode mode = ios_base::in);
Open file
Opens the file identified by argument filename, associating it with the stream object, so that input/output operations are performed on its content. Argument mode specifies the opening mode.

If the stream is already associated with a file (i.e., it is already open), calling this function fails.

The file association of a stream is kept by its internal stream buffer:
Internally, the function calls rdbuf()->open(filename,mode|ios_base::in)

The function sets failbit in case of failure.
The function clears the stream's state flags on success (setting them to goodbit).
In case of failure, failbit is set.

Parameters

filename
String with the name of the file to open.
Specifics about its format and validity depend on the library implementation and running environment.
mode
Flags describing the requested i/o 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
in *inputFile 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 (|).
* in is always set for basic_ifstream objects (even if explicitly not set in argument mode).
Note that even though basic_ifstream is an input stream, its internal basic_filebuf object may be set to also support output operations.

If the mode has app set, the opening operation fails. It also fails if trunc is set but out is not.
If the mode has both trunc and app set, the opening operation fails. It also fails if trunc is set but out is not.

Return Value

none

If the function fails to open a file, the failbit state flag is set for the stream (which may throw ios_base::failure if that state flag was registered using member exceptions).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// print the content of a text file.
#include <iostream>     // std::cout
#include <fstream>      // std::ifstream

int main () {
  std::ifstream ifs;

  ifs.open ("test.txt", std::ifstream::in);

  char c = ifs.get();

  while (ifs.good()) {
    std::cout << c;
    c = ifs.get();
  }

  ifs.close();

  return 0;
}

Data races

Modifies the basic_ifstream object.
Concurrent access to the same stream object introduce data races.

Exception safety

Basic guarantee: if an exception is thrown, the stream is in a valid state.
It throws an exception of member type failure if the function fails (setting the failbit state flag) and member exceptions was set to throw for that state.

See also