ofstream exception handling

前端 未结 3 1264
梦毁少年i
梦毁少年i 2021-02-19 19:53

Deliberately I\'m having this method which writes into a file, so I tried to handle the exception of the possiblity that I\'m writing into a closed file:

void pr         


        
3条回答
  •  醉话见心
    2021-02-19 20:25

    consider following:

    void printMe(ofstream& file)
    {
            file.exceptions(std::ofstream::badbit | std::ofstream::failbit);
            try
            {
                file << "\t"+m_Type+"\t"+m_Id";"+"\n";
            }
            catch (std::ofstream::failure &e) 
            {
                std::cerr << e.what() << std::endl;
            }
    };
    

提交回复
热议问题