ifstream and ofstream or fstream using in and out

前端 未结 2 910
野的像风
野的像风 2021-02-20 04:02

When dealing with files, which of the two examples below is preferred? Does one provide better performance than the other? Is there any difference at all?

ifstr         


        
2条回答
  •  渐次进展
    2021-02-20 04:56

    Just use the more concise form unless you need different behaviour... to do otherwise is just to create room for more errors. FWIW, when possible I prefer to scope the stream and check the open worked like this:

    if (std::ifstream input{"input_file.txt"})
        ...use input...
    else
        ...log and/or throw...
    

提交回复
热议问题