Trying to remove/delete a file using the function below. Unfortunately, it fails every time. Had cut many lines of code that seemed unrelated to the question.
int main()
{
Login();
}
the function below checks the existence of the file. if exists, it can remove it.
void Login()
{
char name[20], password[20], filepassword[10], ID[10], id1[10];
cout << "Login-->\nEnter user-ID: ";
cin >> ID;
strcpy(id1, ID);
strcat(ID,".txt"); //ID is the name of the file (.txt format).
ifstream inputfromfile;
inputfromfile.open(ID);
if(!inputfromfile)
{
cout <<"\nInvalid ID.\n";
exit(EXIT_FAILURE);
}
//IF FILE EXISTS, BELOW IT WILL BE EXECUTED
cout << "Password: "; cin >> password;
inputfromfile filepassword;
if (strcmp(password, filepassword) != 0) //if password doesn't match
{
cout<< "Wrong Password.\n";
perror("Password incorrect");
}
inputfromfile.close();
cout <<"\nEnter 5 to remove account.\n";
int x; cin >> x;
if (x == 5)
{
RemoveAccount(ID);
}
}
remove function. it deletes the file/ID (.txt) that exists in the same folder.
void RemoveAccount(char ID[])
{
if (remove(ID)!=0)
{
cout << "failed to delete!";
perror("account remove failed");
}
else
{
cout << "Successfully deleted!";
exit(EXIT_SUCCESS);
}
}
But if i write remove("jackson.txt")
instead of remove(ID)
, it works successfully. jackson.txt exists in the same folder.