Deleting records from txt file

is this the right codes for deleting a recoed on the text file.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void dlte(){
  string line, recorddate;
  cout << "Please Enter the date of record you want to delete: ";
  cin >> recorddate;
  ifstream myfile;
  ofstream temp;
  myfile.open("herald.txt");
  temp.open("temp.txt");
  while (getline(myfile, line))
  {
    if (line != recorddate)
      temp << line << endl;
  }
  cout << "The record with the date " << recorddate << " has been deleted if it exsisted" << endl;
  myfile.close();
  temp.close();
  remove("herald.txt");
  rename("temp.txt", "herald.txt");
	}
This code if (line != recorddate) compares the entire line, to see whether it matches the string entered by the user. If each line consists of nothing but the date, and the user happens to correctly guess the required date format it might possibly work.
Topic archived. No new replies allowed.