How to delete data from textfile??

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 
can show how?? thanks
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;
}

thats my current code
So, do you have any problems with it?
yeah, mine doesn't do anything after i enter the userid & password
doesn't do anything after i enter the userid & password
SO it just hangs indefinetly and does not output anything, nor finishes?
yeah it just hangs there...
Well, hang there is because you are looping on eof(). It is almost always wrong.
http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong
http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong

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:
1
2
remove("account.txt");
rename("temp.txt","account.txt");
See the problem?
yesss..both my account.txt & temp.txt are both empty
Look what file you are opening and what file you are removing. See the difference?
Topic archived. No new replies allowed.