64,642
社区成员
发帖
与我相关
我的任务
分享
const Book mark = {"0","0","0","0","0","0"}; //empty record
const char* bookDat = "D:\\book.dat";
bool flag = true;
void adminWidget::Initial()
{
QMessageBox box;
box.setWindowTitle("Warning");
box.setIcon(QMessageBox::Warning);
box.setText("If data file exists , it will delete original data.\nContinue ?");
box.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
if(box.exec() == QMessageBox::Yes)
{
ofstream bdat(bookDat,ios::out | ios::binary);
if(!bdat)
{
QMessageBox::warning(this,tr("Warning"),tr("The file does not exist!"),QMessageBox::Yes);
flag = false;
return;
}
bdat.seekp(0, ios::beg);
bdat.write((char*) & mark, sizeof(Book));
bdat.close();
}
}
void adminWidget::Append()
{
Book book;
fstream bdat(bookDat,ios::in | ios::out | ios::binary);
if(!bdat)
{
QMessageBox::warning(this,tr("Warning"),tr("The file does not exist!"),QMessageBox::Yes);
return;
}
bdat.seekg(0,ios::beg);
do
{
bdat.read((char*)&book,sizeof(Book));
}while(!endMark(book));
bdat.seekp(-long(sizeof(Book)),ios::cur);
book.number = ui->bookNum->text();
book.bookName = ui->bookName->text();
book.Author = ui->Author->text();
book.publish_company = ui->publishingCom->text();
book.quantity = ui->bookQuantity->text();
book.price = ui->price->text();
bdat.write((char*)&book,sizeof(Book));
bdat.write((char* )&mark,sizeof(Book));
bdat.close();
}