int main(){
std::ifstream in2(“text.txt”, std::ios::in | std::ios::out);
std::ostream out2(in2.rdbuf()); //shared streambuf
std::cout << in2.rdbuf(); // set the input position indicator to ios::end
out2 << “aaaaaaaaaaaaaaaaa”; // the text inserted in end
out2.seekp(0, std::ios::beg); // set the output position indicator to ios::beg
out2 << “BBBBBBBBBBBBBBBBB”; // replace the text in beg
in2.seekg(0, std::ios::beg); std::cout << in2.rdbuf(); //Using seekg the input positon set to ios::beg
//otherwise the input postion is after “BBBBBBBBBBBBBB”
sets the input position indicator to position off, relative to position, defined by dir. Specifically, executes rdbuf()->pubseekoff(off, dir, std::ios_base::in).