cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : IOstream Library : ofstream : is_open
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
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
ofstream
· ofstream::ofstream
member functions:
· ofstream::close
· ofstream::is_open
· ofstream::open
· ofstream::rdbuf

-

ofstream::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

// ofstream::is_open
#include <iostream>
#include <fstream>
using namespace std;

int main () {

  ofstream outfile;
  outfile.open ("test.txt");
  if (outfile.is_open())
  {
    outfile << "Output operation successfully performed\n";
    outfile.close();
  }
  else
  {
    cout << "Error opening file";
  }
  return 0;
}

This example uses is_open to check whether the file has successfully been opened; If so, it writes a sentence to the file, otherwise it prints out an error message.

Basic template member declaration

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

See also

ofstream::open Open file (public member function)
ofstream::close Close file (public member function)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us