File Handling

HI..
I'm working on an initial level project
using c++ and file handling
i need a help
can anyone guide me
i want to delete the record of an employee wat will b the syntax for this
coz i'm having some problem in this piece of code

int employee::retemp_ID()
{
return emp_ID;
}

void delete_emp(int n)
{
employee e;
ifstream inFile;
ofstream outFile;
inFile.open("account.txt",ios::binary);
if(!inFile)
{
cout<<"File could not be open !! Press any Key...";
return;
}
outFile.open("Temp.dat",ios::binary);
inFile.seekg(0,ios::beg);
while(inFile.read((char *) &e, sizeof(employee)))
{
if(e.retemp_ID()!=n)
{
outFile.write((char *) &e, sizeof(employee));
}
}
inFile.close();
outFile.close();
remove("account.txt");
rename("Temp.dat","account.txt");
cout<<"\n\n\tRecord Deleted ..";

}
Please use code tags

Your code in code tags (and slightly formatted) :
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
int employee::retemp_ID() {
	return emp_ID;
}

void delete_emp(int n) {
	employee e;
	ifstream inFile;
	ofstream outFile;
	inFile.open("account.txt",ios::binary);
	if(!inFile) {
		cout<<"File could not be open !! Press any Key...";
		return;
	}
	outFile.open("Temp.dat",ios::binary);
	inFile.seekg(0,ios::beg);
	while(inFile.read((char *) &e, sizeof(employee))) {
		if(e.retemp_ID()!=n) {
			outFile.write((char *) &e, sizeof(employee));
		}
	}
	inFile.close();
	outFile.close();
	remove("account.txt");
	rename("Temp.dat","account.txt");
	cout<<"\n\n\tRecord Deleted ..";
}
}
Last edited on
I hope "account.txt" is not a text file. Must be binary file with record length = sizeof(employee).
I have used the above code but it's still not deleting the record of employee
Topic archived. No new replies allowed.