This is my code:
void removeAppointments()
{
vector <string> newfile;
string line;
int i;
int n;
ifstream Appointments;
ofstream newAppointments;
Appointments.open(DATAFILE);
newAppointments.open(newDATAFILE);
if (Appointments.is_open() && newAppointments.is_open())
{
// Read the file until it's finished, displaying lines as we go.
while (!Appointments.eof())
{
getline(Appointments, line, '\n'); // getline reads a line at a time
newfile.push_back(line);
}
while (n != i && n <= newfile.size())
{
n = 0;
newAppointments << newfile[n] << '\n';
n++;
}
}
else // Some kind of error opening the file
{
cout << "ERROR: Could not open the datafile(s)" << endl;
exit(1);
}
newAppointments.close();
Appointments.close();
remove(DATAFILE);
rename(newDATAFILE, DATAFILE);
}
int main(int argc, char *argv[])
{
//Appointment appointment;
int i = atoi(argv[1]) - 1;
removeAppointments();
}
However when I use the program, it simply deletes the entire datafile.