C++ basic_filebuf_is_open



Description

The C++ function is_open() returns true or false based on the object's current association to a file.

Declaration

Following is the declaration for is_open() function.

bool is_open() const

Parameters

  • NA

Return Value

This function returns true if file is open and associated with file stream buffer object, else returns false.

Example

The following example shows the usage of is_open() function.

#include <iostream.h>
#include <fstream.h>
#include <conio.h>

int main () {

  ifstream i;
  char s[10];

  // streambuf to handle file
  filebuf *f = i.rdbuf();
  // file opened
  f->open ("input.txt", ios::in);

  if(f->is_open())
    cout<<"file opened";
  else
    cout<<"file does not exist, create new file";

  f->close();

  return(0);
}

Let us compile and run the above program, this will produce the following result:

file opened
Advertisements