// Code derived from Stroustrup's PPP2 book// § 10.4 Opening a file// -and beginning on p 352#include<fstream>#include<iostream>#include<stdexcept>#include<string>usingnamespacestd;voiderror(conststrings){throwruntime_error(s);}voiderror(conststrings1,conststrings2){error(s1+s2);}//------------------------------------------------------------------------------intmain()try{ifstreamifs;stringname="chapter.10.4-problematic.foo";// ...stringfoo;ifs>>foo;// won't succeed: no file opened for ifs// ...ifs.open(name,ios_base::in);// open file named name for reading// ...ifs.close();// close file// ...stringbar;ifs>>bar;// won't succeed: ifs' file was closed// ...fstreamfs;fs.open("foo",ios_base::in);// open for input// close() missingfs.open("foo",ios_base::in);// won't succeed: ifs is already openif(!fs)error("impossible");}catch(exception&e){cerr<<"error: "<<e.what()<<'\n';return1;}catch(...){cerr<<"Oops: unknown exception!\n";return2;}