Problems opening a binary file

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
void eraseRecord(fstream & custFile)
{
       fstream custFileMod;
       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);
       recNum = 0;
       while(!custFile.eof())
       {
          //Seek the record.
          custFile.seekg(recNum * sizeof(record),ios::beg);
          if(custFile.fail())
          {
             cout << "Error locating.";
             custFile.close();
          }
     custFile.read(reinterpret_cast<char*>(&record),sizeof(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();
         
}


equal is a function that searches through the file and finds the matching file name. Please, can you tell me what's wrong?
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
Thank you for your assistance. I will fix it now and see if there are other mistakes to correct.
I fixed that, but it still does not erase the record. Here's the edited code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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?
You don't need to manually seek anything. Read will advance the get pointer for you.

Your logic should be:

read a record (and check if reading was successful),
check if it's the one you were looking for,
if it isn't, write it to the other file,
repeat.

Now the second file equals the first one except that one record you didn't write.
Thanks. I'll be fixing it now. Hopefully, i won't find any more errors.
Topic archived. No new replies allowed.