// Code derived from Stroustrup's PPP2 book// § 11.3.3 Positioning in files// -and beginning on p 393#include<fstream>#include<iostream>#include<stdexcept>#include<string>usingnamespacestd;voiderror(stringconst&s){throwruntime_error(s);}voiderror(stringconst&s1,stringconst&s2){error(s1+s2);}//------------------------------------------------------------------------------intmain()try{stringconstname{"points.txt"};fstreamfs{name};// open for input and outputif(!fs)error("can't open ",name);fs.seekg(5);// move reading position (g for “get”) to 5 (the 6th character)charch;fs>>ch;// read and increment reading positioncout<<"character[5] is '"<<ch<<"' ("<<int(ch)<<")\n";fs.seekp(1);// move writing position (p for “put”) to 1fs<<'4';// write and increment writing position}catch(exceptionconst&e){cerr<<"error: "<<e.what()<<'\n';return1;}catch(...){cerr<<"Oops: unknown exception!\n";return2;}