Hi Friends,
I know this question would be answered many times, but I am not able to find its solution. Please help me,,,

So, the problem is my program is writing a binary file, Then this file is given input to another file, which has to change the content of some objects that are there in the file.
So, for example
writing function is:
Code:
class Object
{
      public:
          int x;
          int y;
     
};
int main()
    {
           Object ob;
           ofstream myFile("test.bin",ios::out|ios::app|ios::binary);
           for(int i=0;i<=10;i++)
           {
               ob.x = i;
               ob.y = i*2;
               myFile.write((char *)&ob,sizeof(class Object));
           }
           myFile.close();
    }

and the other program which takes the file and then process it is:

class Object
{
      public:
          int x;
          int y;    
};

void readFromLocation(unsigned long int i){
       Object ob;
       ifstream myFile("test.bin",ios::in|ios::binary);
       if(myFile){
                  i = i*sizeof(class Object);
            myFile.seekg(i,ios::beg);
            myFile.read((char *)&ob,sizeof(class Object));
            cout<<ob.x<<"  "<<ob.y<<"\n";
            }
       myFile.close();
       }


void changeFileContentsAtParticularLocation(unsigned long int i,int x,int y)
{
     Object ob;
     ob.x = x;
     ob.y = y;
       ofstream myFile("test.bin",ios::out|ios::binary);
       if(myFile){
                  cout<<"Size of Class Object before everything" <<sizeof(class Object)<<"\n";
            i = i*sizeof(class Object);
            myFile.seekp(i,ios_base::beg);
            cout<<myFile.tellp()<<"\n";
            myFile.write((char *)&ob,sizeof(class Object));
            cout<<"Size of the class object is  "<<sizeof(class Object)<<"\n";
            cout<<myFile.tellp()<<"\n";
            }
       myFile.close();
}
void testReadWholeFile()
{
     Object ob;
     ifstream myFile("test.bin",ios::in|ios::binary);
     if(myFile){
                  for (int k =0;k<=10;k++){
                              myFile.read((char *)&ob,sizeof(class Object));
                              cout<<ob.x<<"  "<<ob.y<<"\n";
                              }
            }
       myFile.close();
}

int main(){
    int operation;
    unsigned long int locationToSee;
    unsigned long int locationToChange;
   
#if 1
    while(1){
    cout<<"Enter 1 to see and 2 to change \n";
    cin>>operation;
    testRead();
    switch(operation){
                      case 1:
                           cout<<"Enter the Location Which you want to see\n";
                           cin>>locationToSee;
                           readFromLocation(locationToSee);
                           break;
                      case 2:
                           cout<<"Enter the Location whose values you want to change\n";
                           cin>>locationToChange;
                           cout<<"Enter X\n";
                           int x,y;
                           cin>>x;
                           y=x*2;
                           changeFileContentsAtParticularLocation(locationToChange,x,y);
                           break;
                      default:
                           break;
                           }
  
        }
#endif
return 0;
}
So, in my second program when I try to change the content of particular object, it doesn't work.
Please help me with that,
Arun