删除书.cpp

来自「图书馆管理系统 查找」· C++ 代码 · 共 72 行

CPP
72
字号
#include "stdafx.h"
#include"harRange.h"
#include"BookData.h"
void deleteBook()
{
	fstream file;
	file.clear();

	fstream file2;
	BookData obj;
	string search,oldName;
	file.open("shendan.dat",ios::in|ios::out|ios::binary);
	if(file.fail())
	{
		cout<<"\t\t\t打开文件失败!\n";
		exit(0);
	}
	file2.open("shen.dat",ios::out|ios::app|ios::binary);
	if(file2.fail())
	{
		cout<<"\t\t\t打开文件失败!\n";
		exit(0);
	}
	cout<<"\t\t\t---------------------------\n";
	cout<<"\t\t\t请输入您要查找的书名:";
	cin>>search;
   	file.seekg(0L,ios::beg);
	file.read((char*)&obj,sizeof(obj));
	while(!file.eof())
	{
		
		oldName.assign(obj.getTitle ());
		if(search!=oldName)
		{
			file2.write((char*)&obj,sizeof(obj));	
		}
		file.read((char*)&obj,sizeof(obj));
	}
	file.clear();
	file2.clear();
	file.close();
	file2.close();
	remove("shendan.dat");

	file.open("shendan.dat",ios::out|ios::app|ios::binary);
	if(file.fail())
	{
		cout<<"打开文件失败!\n";
		exit(0);
	}

	file2.open("shen.dat",ios::in|ios::binary);
	if(file2.fail())
	{
		cout<<"打开文件失败!\n";
		exit(0);
	}
	file2.read((char*)&obj,sizeof(obj));
	while(!file2.eof())
	{


		file.write((char*)&obj,sizeof(obj));	
		file2.read((char*)&obj,sizeof(obj));

	}

	file.close();
	file2.close();
	remove("shen.dat");
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?