Checking if a file opened successfully with ifstream

后端 未结 4 1256
我在风中等你
我在风中等你 2020-12-10 01:28

I have the following that will open a file for reading. However, I want to check to make sure that the file was open successfully, so I am using the fail to see if the flags

4条回答
  •  一个人的身影
    2020-12-10 02:22

    You have to call fail() on the stream object. A more idiomatic way of doing this is:

    input_stream.open(_file_name.c_str(), ios::in);
    
    if( ! input_stream ) {
        return -1;
    }
    

提交回复
热议问题