i'm trying to delete an account from account.txt.My program has a login fuction which compares user id & password with account.txt. i want to delete an account but i'm not sure how. somebody please help me !!!!
There is no way to delete arbitrary part of file. Usually you just copy that file without deleted part.
Common way to do so:
(to delete account 2)
Open account.txt with accounts 1 2 and 3
Save data for accounts 1 and 3 to account.txt.temp
Delete account.txt
Rename account.txt.temp to account.txt
void deleteAccount ()
{
system("cls");
ifstream inFile;
string id,pass,tid,tpass;
int x=0;
cout << "Enter Account user id and pass that you want to remove from database" << endl;
cin >> tid >>tpass;
inFile.open("account");
ofstream tempFile("temp.txt");
while(!inFile.eof())
{
inFile >> id >> pass;
if(tid!=id && tpass!=pass)
tempFile << id << ' ' << pass << endl;
else
x=1;
}
inFile.close();
tempFile.close();
remove("account.txt");
rename("temp.txt","account.txt");
if(x==0)
{
cout << "There is no account with name you entered." << endl;
}
else
{
cout << "Student data has been deleted." << endl;
}
return;
}
Use while(inFile >> id >> pass) to solve hanging problem.
You will encounter nex problem: account.txt will be empty after you run the program.
This is because of that line: inFile.open("account");. Compare with these lines: