"kathy" <yq*****@yahoo.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
I have code piece:
...
std::fstream outFile(_T("data.txt"),std::ios::out|std::ios::tru nc);
Note that the '_T' macro is not part of standard C++ (and
is not germane to your question).
if(outFile)
{
...
}
When I build it, I got:
"c:\temp\DialogDemoDlg.cpp(202): error C2451: conditional expression of
type 'std::fstream' is illegal."
Why?
There's an idiom (and a supporting member function) for checking
the state of a stream, which allows a stream object to be used
in a boolean context. But converting it to a boolean value
doesn't 'just happen'. To do so requires the use of a boolean
logical operator (a stream's 'value' itself is not boolean).
(The member function is 'operator void*', you can research it
if you like).
Try this
if(!outFile)
// stream in is error state
else
// stream is in 'good' state
If you want the first test to be for 'good' state, you
can write:
if(!!outFile)
or:
if(outFile.good())
BTW if you only need output, you should use 'std::ofstream'.
'std::fstream' is intended for streams which are both read
from and written to.
-Mike