双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

第二次打开文件无法写入。。。

zhangwh1991 zhangwh1991 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

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();
}


先调用Initial()初始化文件,可正常调用Append()写文件。。第二次开始就不必调用Initial(),直接调用Append()。。。出错了。。为什么?哪里有问题?望指教!
...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
zhangwh1991
C++ 语言 65,214 成员 250,514 内容