void modifySach()
{
char n[10];
int found=0;
system("cls");
cout<<"\n\n\tSUA THONG TIN SACH.... ";
cout<<"\n\n\tNhap ma sach: ";
gets(n);
fp.open("sach.dat",ios::in|ios::out);
while(fp.read((char*)&Book,sizeof(Sach)) && found==0)
{
if(strcmp(Book.getMaS(),n)==0)
{
titleSach();
Book.editSach();
int pos=-1*sizeof(Book); // giai thichho em doan code nay duoc khong a
fp.seekp(pos,ios::cur);
fp.write((char*)&Book,sizeof(Sach));
cout<<"\n\n\t Record Updated";
found=1;
}
}
fp.close();
if(found==0)
cout<<"\n\n Record Not Found ";
cout<<endl; system("pause"); cout<<endl;
}
Thắc mắc ý nghĩa của câu lệnh pos = -1 * sizeof(Book)
Đoạn đó là làm rườm rà cho khó hiểu.
Viết gọn thành:
fp.seekp(-sizeof(Book), ios:cur);
Dịch con trỏ ngược về đầu cuốn sách cần sửa để ghi lại nội dung của nó khi gọi fp.write((char*)Book, sizeof(Sach));
.
4 Likes