Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
IOstream Library
manipulators
classes:
filebuf
fstream
ifstream
ios
iostream
ios_base
istream
istringstream
ofstream
ostream
ostringstream
streambuf
stringbuf
stringstream
objects:
cerr
cin
clog
cout
types:
fpos
streamoff
streampos
streamsize
ifstream
ifstream::ifstream
member functions:
ifstream::close
ifstream::is_open
ifstream::open
ifstream::rdbuf


ifstream::is_open

public member function
bool is_open ( );

Check if a file is open

Returns true if the stream is currently associated with a file, and false otherwise.

To determine this, the function calls: rdbuf()->is_open()

The stream is associated with a file if either a previous call to member open succeeded or if the object was successfully constructed using the parameterized constructor, and close has not been called since.

Parameters

none

Return Value

true if a file is open, i.e. associated to this stream object.
false otherwise.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// ifstream::is_open
#include <iostream>
#include <fstream>
using namespace std;
int main () {
  ifstream infile;
  infile.open ("test.txt");
  if (infile.is_open())
  {
    while (infile.good())
      cout << (char) infile.get();
    infile.close();
  }
  else
  {
    cout << "Error opening file";
  }
  return 0;
}


This example uses is_open to check whether the file has successfully been opened, if so prints out the content of the file, otherwise -like for example if the file to be opened doesn't exist- an error message is shown.

Basic template member declaration

( basic_ifstream<charT,traits> )
 
bool is_open ();


See also