Modify a binary file

Hi,
I'm making a phone-book,i wanna edit a record of person.I tried to use seekp function to do so but failed,i'm using ios::app mode.
Guide me how to replace some data in the file.
If you want i'll send you complete program

void Update()
{
system("cls");
char Name[30];
int count=0,PointerPosition=0;
cout<<"Enter Your Full Name :";
cin.ignore();
gets_s(Name);
in.open("AddressBook.dat",ios::binary);
while(!in.eof())
{
in.read((char*)&Record,sizeof(Record));
if(strcmp(Name,Record.GetName())==0)
{
Record.update();/*this will update the Record*/
break;
}
count++;
}
in.close();
in.clear();
out.open("AddressBook.dat",ios::ate|ios::app|ios::binary);
PointerPosition=count*sizeof(Record);
out.seekp(PointerPosition);
out.write((char*)&Record,sizeof(Record));
out.close();
}
Remove ios::ate option while opening for editing at middle of the file, because it allows to append end of the file only.
i removed ios::ate
but this did not fulfil my requirement, i want to replace data actually.
Use the following example which written by using FILE.
But be sure that file should be present before you are going to open it.
1
2
3
4
5
6
7
8
9
10
11
 void main()
 {
	 FILE *file;
	 file=fopen("C:\\bin.dat","rb+");
	 string str="onetwothree";
	 fwrite(str.c_str(),1,str.size(),file);
	 str="123";
	 fseek(file,3,SEEK_SET);
	 fwrite(str.c_str(),1,str.size(),file);
	 fclose(file);
 }


Topic archived. No new replies allowed.