problem in modifying record in binary file

modify function is working fine for any record in file. only problem is that when i modify last record it works but showing extra garbage value ... please help...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void modify_account(int n)
{
	bool found=false;
	account ac;
	fstream File;
    File.open("account.dat",ios::binary|ios::in|ios::out);
	if(!File)
	{
		cout<<"File could not be open !! Press any Key...";
		return;
	}
	int pos=File.tellp();
    while(File.read(reinterpret_cast<char *> (&ac), sizeof(account)) && found==false)
	{
		if(ac.retacno()==n)
		{
			ac.show_account();
			cout<<"\n\nEnter The New Details of account"<<endl;
			ac.modify();
			File.seekp(pos,ios::beg);
		    File.write(reinterpret_cast<char *> (&ac), sizeof(account));
		    cout<<"\n\n\t Record Updated";
		    found=true;
		  }
	}
	File.close();
	if(found==false)
		cout<<"\n\n Record Not Found ";
}
Last edited on
I think you should put "nul-terminating" character when finished writing to file.
Line 13 is wrong. You probably want to change it to
while(File.read(reinterpret_cast<char *> (&ac) && found==false)
Topic archived. No new replies allowed.