testing for std::ofstream errors?

R

Roy Smith

I've got some old code I'm working on which looks like this:

std::eek:fstream fstr;
fstr.open(filename, mode);
if (!fstr) {...}

I don't understand the if(!fstr) test. From what I can see in the
references I've got handy, that should be if(fstr.fail()) {...}, but
the current code seems to work. Is this correct/portable code?
 
S

Saeed Amrollahi

I've got some old code I'm working on which looks like this:

        std::eek:fstream fstr;
        fstr.open(filename, mode);
        if (!fstr) {...}

I don't understand the if(!fstr) test.  From what I can see in the
references I've got handy, that should be if(fstr.fail()) {...}, but
the current code seems to work.  Is this correct/portable code?

Hi Roy

the above code is OK. It's both correct and portable. Actually, the
overloaded ! operator
call fail member function:
template<class Ch>
class basic_ios : public ios_base {
public:
// ...
bool operator!() const { return fail(); }
// ...
};
See the following texts:
Bjarne Stroustrup. The C++ Programming Language. section 21.3.3
and
C++ Standard Document. section 27.4.4.3

Regards,
-- Saeed Amrollahi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top