ofstream/ostream flush() error check

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pnayak
    New Member
    • Apr 2008
    • 9

    #1

    ofstream/ostream flush() error check

    Is this method of checking, if flush() succeeded or failed, valid?

    Code:
    std::ofstream ofs;
    file.open("test.txt")
    
    file << "line of text";
    
    if (!ofs.flush()) {
        std::cout << " failed to write \n ";
        return 1;
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    The flush() member function returns an ostream& so it is pointless to check for a null return.

    If the function does not throw an exception, then you have to assume it succeeded.

    Comment

    Working...