ifstream: check if opened successfully

后端 未结 4 2017
猫巷女王i
猫巷女王i 2020-12-14 14:11

A colleague just told me that this code:

std::ifstream stream(filename.c_str());
if (!stream)
{
    throw std::runtime_error(\"..\");
}

wou

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-14 15:14

    Your colleague is wrong. Perhaps he's forgotten that you're not writing C.

    The code is spot on. It's exactly how you should be checking stream state.

提交回复
热议问题