// Code derived from Stroustrup's PPP2 book// § 10.6 I/O error handling// -and beginning on p 356#include<iostream>#include<stdexcept>#include<string>#include<vector>usingnamespacestd;voiderror(conststrings){throwruntime_error(s);}voiderror(conststrings1,conststrings2){error(s1+s2);}//------------------------------------------------------------------------------// read integers from ist into v until we reach eof() or terminatorvoidfill_vector(istream&ist,vector<int>&v,charterminator){for(intI;ist>>I;)v.push_back(I);if(ist.eof())return;// fine: we found the end of fileif(ist.bad())error("ist is bad");// stream corrupted; let’s get out of here!if(ist.fail()){// clean up the mess as best we can and report the problemist.clear();// clear stream state, so that we can look for terminatorcharc;ist>>c;// read a character, hopefully a terminatorif(c!=terminator){// unexpected characterist.unget();// put that character backist.clear(ios_base::failbit);// set the state to fail()}}}intmain()try{vector<int>v;fill_vector(cin,v,'*');}catch(exception&e){cerr<<"error: "<<e.what()<<'\n';return1;}catch(...){cerr<<"Oops: unknown exception!\n";return2;}