void del

Be the best man win
 avatar
unknown
c_cpp
10 months ago
1.3 kB
11
Indexable
void del() {
	int id;
	char found = 'f';
	ifstream ifile("records.dat", ios::in | ios::binary);
	ifstream infile;
	ofstream outfile;
	
	sys('c');
	
	if (ifile) {
		ifile.close();
		cout << appname << margin << border << endl
			 << margin << "|" << setw(55) << "---| DELETE EMPLOYEE DATA |---" << setw(25) << "|" << endl
			 << margin << border << endl;
		user_input("Enter employee's id");
		cin >> id;
			
		infile.open("records.dat", ios::in | ios::binary);
		outfile.open("temp.dat", ios::binary);
		infile.seekg(0, ios::beg);
		while(infile.read((char *)&data, sizeof(data))) {
			if(return_id() != id) {
				outfile.write((char *)&data, sizeof(data));
			} else {
				found = 't';
			}
		}
		infile.close();
		outfile.close();
		
		remove("records.dat");
		rename("temp.dat", "records.dat");
	} else {
		cout << appname;
		display_resp("404 No Record!", "failed");
		found = 'x';
	}
	
	if (found == 't') {
		display_resp("Record has been Deleted!", "success");
		ifstream file("records.dat", ios::in | ios::binary);
		file.seekg(0, ios::end);
		if (file.tellg() == 0) {
			file.close();
		    remove("records.dat");
		}
	} else if (found == 'f') {
		display_resp("Record Not Found!", "failed");
	}
	
	getch();
}
Editor is loading...
Leave a Comment