Question:

How Do I Delete a Structure record from a .dat File in a C++ Program?

by  |  earlier

0 LIKES UnLike

I am writing a Database for a book store in a DOS console program, and I am having trouble with the Delete book function. I can successfully search the file for the book I am trying to remove and pass the location in the file to the Delete function. but when it tries to delete it, my program bugs and the screen starts scrolling and it corrupts the .dat file, and I have to delete it.

The code I am working with is as follows (this will not compile, I have copied and pasted the code of interest) :

struct items

{

char isbn[30];

char title[30];

char author[30];

char publisher[30];

char date[20];

int onHand;

double retail;

double wholesale;

};

void deletebook()

{

char title[50];

int choice=0;

char response ='n';

//book entry prompt

cin.ignore();

cout<<"\n Enter the title as it appears in file: ";

cin.getline(title, 50);

iFile.open("database.dat",ios::in);

if(iFile.fail())

{

iFile.open("database.dat", ios::out);

iFile.close();

iFile.clear();

}

else

{

while(!iFile.eof())

{

for(int fromFile=0; fromFile<50; fromFile++)

{

int IfromFile=fromFile;

iFile.seekg(IfromFile*sizeof(book),ios...

iFile.read(reinterpret_cast<char*>(&bo... sizeof(book));

book[fromFile];

}

}

iFile.close();

iFile.clear();

}

for (int row=0; row <50; row++)

{

if (strstr(book[row].title, title))

{

cout<<"\nIs......"

<<book[row].title

<<"\n\n";

cout<<"This your book?\n";

cout<<"1. yes\n";

cout<<"2. no\n\n";

cout<<"Which is it?";

cin>> choice;

if (choice ==1)

{

cout<<"Delete this Book(Y/N): ";

cin>>response;

while(response !='y'&&response !='Y'&&response != 'n'&& response !='N')

{

cout<<"\nPlease enter \'Y\' for Yes or \'N\' for No.\n\n";

cout<<"would you like to delete this entire record?(Y/N): ";

cin>> response;

}

if (response=='Y'||response=='y')

{

removeBook(row);

}

//Removal function

void removeBook(int subscript)

{

int Isubscript=subscript;

iFile.open("database.dat",ios::out);

iFile.seekp(Isubscript*sizeof(book),io...

book[Isubscript].title[0]='\0';

iFile.write(reinterpret_cast<char*>(&b...

iFile.close();

iFile.clear();

}

 Tags:

   Report

2 ANSWERS


  1. Hi

    From experience I would strongly advise that if you are deleting and adding  records to a text based datafile to copy it first to a temporary file, do whatever  and when you have finished and satisfied copy it back to the original file and delete the temporary one.

    I know this does not quite answer your question, just a word of advise.

    Regards


  2. Can&#039;t tell for sure, as YA chopped out significant portions of your code. However, opening the file as output when you&#039;re getting ready to delete seems suspicious to me. Is your delete process intended to just set a delete flag for eventual compression or write out a copied version from a buffer while skipping the record to delete?

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions