// Code derived from Stroustrup's PPP2 book// § 10.4 Opening a file// -and beginning on p 351#include<fstream>#include<iomanip>#include<iostream>#include<stdexcept>#include<string>#include<vector>usingnamespacestd;voiderror(conststrings){throwruntime_error(s);}voiderror(conststrings1,conststrings2){error(s1+s2);}//------------------------------------------------------------------------------structPoint{intx;inty;friendistream&operator>>(istream&ist,Point&p){chara,b,c;if((ist>>a>>p.x>>b>>p.y>>c)&&!(a=='('&&b==','&&c==')'))error("Invalid format");returnist;}friendostream&operator<<(ostream&ost,constPoint&p){returnost<<'('<<setw(2)<<p.x<<','<<setw(2)<<p.y<<')';}};intmain()try{cout<<"Please enter name of output file: ";stringoname;cin>>oname;ofstreamost{oname};// ost is an output stream for a file named onameif(!ost)error("can't open output file ",oname);vector<Point>points;for(inti=0;i<10;++i){Pointp={i,i*i};points.push_back(p);}for(Point&p:points)ost<<'('<<p.x<<','<<p.y<<")\n";}catch(exception&e){cerr<<"error: "<<e.what()<<'\n';return1;}catch(...){cerr<<"Oops: unknown exception!\n";return2;};voidfill_from_file(vector<Point>&points,string&name){ifstreamist{name};// open file for readingif(!ist)error("can't open input file ",name);// . . . use ist . . .for(auto&p:points)// trivial usecout<<p<<'\n';// the ifstream file is implicitly closed here when we leave this function}