i have tried this code, it works fine.
make sure the content of text.txt is "this is an apple" only.
Code:
#include <fstream>
#include<iostream>
using namespace std;
int main (int argc, char *argv)
{
long pos;
fstream outfile;
outfile.open ("test.txt",ios::out|ios::ate);
if (outfile.fail())
{
cout << "Error opening file, errno: " << endl ;
}
pos=outfile.tellp();
cout<<"\n pos is " << pos;
outfile.seekp (pos-7);
cout<<"\n pos is " << outfile.tellp();
outfile.write (" sam",4);
outfile.close();
return 0;
}