English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Can someone explain to me how to use
the is_opened method in fstream library? I have a bit of problem with the syntax. The method would return true if the file can be opened or false if it cannot be. Can you give me an example. Thank you.

2007-08-23 08:39:40 · 3 answers · asked by coolbun2003 1 in Computers & Internet Programming & Design

3 answers

bool is_open ( );

Check if a file is open (fstream public member function)

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:

// fstream::is_open
#include
#include
using namespace std;

int main () {

fstream filestr;

filestr.open ("test.txt");

if (filestr.is_open())
{
cout << "File successfully open";
filestr.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_fstream )
bool is_open ();


______________________

2007-08-23 09:31:47 · answer #1 · answered by Einstein 5 · 1 0

No, the method is for calling by a STREAM (i.e. is the stream pointing at a file, or not.

http://www.cplusplus.com/reference/iostream/fstream/is_open.html

2007-08-23 15:46:20 · answer #2 · answered by Kasey C 7 · 0 0

To tell the truth I doubt you ever need to use it. To check if a filestream is open you just need to do either

if (!myStream)

or you could do:

if( myStream.fail() )

hope this helps...

2007-08-23 15:43:36 · answer #3 · answered by icefyre 5 · 0 1

fedest.com, questions and answers