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.
C++ question?
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()-%26gt;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 %26lt;iostream%26gt;
#include %26lt;fstream%26gt;
using namespace std;
int main () {
fstream filestr;
filestr.open ("test.txt");
if (filestr.is_open())
{
cout %26lt;%26lt; "File successfully open";
filestr.close();
}
else
{
cout %26lt;%26lt; "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%26lt;charT,traits%26gt; )
bool is_open ();
______________________
Reply: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/iostr...
Reply: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...
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment