File I/O

Somebody help me with my project.
All of the options work except for the "delete" book option.
Whenever I use this code/function:

system("erase bookfile.txt ");
getch();
system("rename tempfile.txt bookfile.txt");

when compiled, it becomes an error.
But when removed & compiled, it works but can't delete a file.
Btw, I'm using C++ Borland Ver. 4.5. I know it's an outdated version but my school strictly implements that we must use only this.

Here is the Source Code

#include <iostream.h>
#include <fstream.h>
#include <conio.h>
#include <iomanip.h>
#include <stdio.h>
#include <string.h>
#pragma pack (2)

int menu();
class Book
{
private:
int bookid;
char title[20];
float price;
protected:
int allotbookid();
void showheader();
public:
void getbook();
void showbook();
void addbook();
void viewbook();
void searchbook();
void deletebook();
void modifybook();
};
int Book::allotbookid()
{
ifstream fin;
Book temp;
int id=0;
fin.open("bookfile.txt",ios::in|ios::binary);
if(!fin)
return(id+1);
else
{
fin.read((char*)&temp,sizeof(temp));
while(!fin.eof())
{
id=temp.bookid;
fin.read((char*)&temp,sizeof(temp));
}
id++;
return(id);
}
}


void Book::showheader()
{

cout<<"\n"<<setw(10)<<"BOOK ID"<<setw(10)<<"Price"<<setw(10)<<"Title\n";
}
void Book::getbook()
{
cout<<"Enter Book Title: ";
fflush(stdin);
gets(title);
cout<<"Price of Book: ";
cin>>price;
bookid=allotbookid();
}
void Book::showbook()
{

cout<<"\n"<<setw(10)<<bookid<<setw(10)<<price<<setw(10)<<title;
}
void Book::addbook()
{
ofstream fout;
fout.open("bookfile.txt",ios::out|ios::app|ios::binary);
if(!fout)
cout<<"File can not open";
else
fout.write((char*)this,sizeof(*this));
fout.close();
}
void Book::viewbook()
{
ifstream fin;
fin.open("bookfile.txt",ios::in|ios::binary);
if(!fin)
cout<<"File not found";
else
{
showheader();
fin.read((char*)this,sizeof(*this));
while(!fin.eof())
{
showbook();
fin.read((char*)this,sizeof(*this));
}
}
fin.close();
}
void Book::searchbook()
{
ifstream fin;
char str[20];
fin.open("bookfile.txt",ios::in|ios::binary);
cout<<"Enter the name of book to search:";
fflush(stdin);
gets(str);
if(!fin)
cout<<"File not found";
else
{
fin.read((char*)this,sizeof(*this));
while(!fin.eof())
{

if(!strcmp(this->title,str))
{
showheader();
showbook();
break;
}
fin.read((char*)this,sizeof(*this));
}
if(fin.eof())
cout<<"\nRecord not found";
}
fin.close();
}
void Book:: modifybook()
{
int id,r=0;
fstream file;
file.open("bookfile.txt",ios::in|ios::out|ios::ate|ios::binary);
cout<<"\nEnter record number to modify (book ID): ";
cin>>id;
file.seekg(0);
if(!file)
cout<<"File not found";
else
{
file.read((char*)this,sizeof(*this));

while(!file.eof())
{
r++;
if(bookid==id)
{
showheader();
showbook();
cout<<"\nRe-enter book details:\n";
cout<<"Enter book title: ";
fflush(stdin);
gets(title);
cout<<"Enter book price: ";
cin>>price;
file.seekp((r-1)*sizeof(Book),ios::beg);
file.write((char*)this,sizeof(*this));
break;
}
file.read((char*)this,sizeof(*this));
}
if(file.eof())
cout<<"Record not found";
}
file.close();
}
void Book:: deletebook()
{
ifstream fin;
ofstream fout;
int id;
char x;
fin.open("bookfile.txt",ios::in|ios::binary);
fout.open("tempfile.txt",ios::out|ios::app|ios::binary);
cout<<"Enter book ID to delete record: ";
cin>>id;
if(!fin)
cout<<"File not found";
else
{
fin.read((char*)this,sizeof(*this));
while(!fin.eof())
{
if(this->bookid==id)
{
cout<<"Record you want to delete is:\n\n";
showheader();
showbook();
cout<<"\nAre you sure you want to delete this record(y/n): ";
fflush(stdin);
cin>>x;
if(x=='n')
fout.write((char*)this,sizeof(*this));
else
cout<<"\nRecord is deleted";
}
else
fout.write((char*)this,sizeof(*this));
fin.read((char*)this,sizeof(*this));
}
fin.close();
fout.close();


system("erase bookfile.txt ");
getch();
system("rename tempfile.txt bookfile.txt");

getch();



}
}
int menu()
{
cout<<"\t\t\tBethesda Corp.\n";
cout<<"\t\t\tNacorda's Bookshop"<<endl;
cout<<"\n\t1. Add new book";
cout<<"\n\t2. View all books";
cout<<"\n\t3. Search book";
cout<<"\n\t4. Modify book";
cout<<"\n\t5. Delete book";
cout<<"\n\t6. Exit";
cout<<"\n\n\tEnter your choice: ";
int ch;
cin>>ch;
return(ch);
}
int main()
{
clrscr();
Book b;
int ch;
while(1)
{
ch=menu();
switch(ch)
{
case 1:
b.getbook();
b.addbook();
break;
case 2:
b.viewbook();
break;
case 3:
b.searchbook();
break;
case 4:
b.modifybook();
break;
case 5:
b.deletebook();
break;
case 6:
cout<<"\t\tThank you and Enjoy your Book!\n";
cout<<"\t\t\tPls Come Again!\n";
break;


default:
cout<<"Enter Valid choice";
}
getch();
clrscr();
}
}


PLS PLS PLS somebody help me.
This project is due in 12 hrs and I don't know what to do.
Pls help ASAP!!!
>.<
Please edit your post and make sure your code is [code]between code tags[/code] so that it has line numbers and syntax highlighting, as well as proper indentation.

Please copy and paste the exact error message and indicate which line it is referring to.
Topic archived. No new replies allowed.