I have written a program to keep a movie record. All functions work as desired except the function that i wrote to erase a record. I have been debugging and found out that the temporary file that i use to store the data doesn't want to open. Here's the function:
You mean the "custtry1.dat"? If the file doesn't exist and you try to open it with an ios::in flag, it will not be created (which sort of make sense, since you can't read from an empty file).
By the way, you can replace reinterpret_cast<char*>(&record) with (char*)&record
void eraseRecord(fstream & custFile)
{
fstream custFileMod;
custFileMod.open("custtry1.dat", ios::out|ios::binary);
custFileMod.close();
custFileMod.clear();
MovieData record;
long recNum;
char name1[SIZE];
custFile.open("custtry.dat",ios::in|ios::binary);
if(custFile.fail())
{
exit(EXIT_FAILURE);
}
custFileMod.open("custtry1.dat", ios::in|ios::out|ios::binary);
if(custFileMod.fail())
{
exit(EXIT_FAILURE);
}
cin.ignore();
cout << "\nEnter the movie title: ";
cin.getline(name1,SIZE); //Gets the movie title from the user.
recNum = 0; //Sets the record number at 0.
while(!custFile.eof()) //While the loop does not get to the end of the file...
{
//...seek the record.
custFile.seekg(recNum * sizeof(record),ios::beg); // Seek record starting from the
if(custFile.fail()) // beginning of the file.
{
cout << "Error locating.";
custFile.close();
}
custFile.read(reinterpret_cast<char*>(&record),sizeof(record)); //Read from the file into
// the record.
if(equal(record.movieTitle,name1))
{
recNum++;
}
else
custFileMod.read(reinterpret_cast<char*>(&record),sizeof(record));
custFileMod.write(reinterpret_cast<char*>(&record),sizeof(record));
recNum++;
}
custFile.close();
custFileMod.close();
custFile.clear();
}
It is supposed to copy all the records to the custFileMod file and write them all except the selected record back to custFile. Still doesn't work. Any suggestions?