0

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.

Miraz
  • 131
  • 1
  • 9
  • 3
    `"jackson.txt"` is an array containing 12 characters. Your `char` array `ID` is declared as containing `10` `char`s. So `remove("jackson.txt")` would never be equivalent to `remove(ID)`. – Algirdas Preidžius Feb 23 '21 at 15:50
  • 4
    Don't use `char` arrays for strings in C++, use `std::string` instead. It will simplify many things for you. – Some programmer dude Feb 23 '21 at 15:51
  • There are _two_ `remove` functions that I know of. Which one are you using? Since you haven't shown what header files you've included and namespaces you are `using` - it's impossible to say. – Ted Lyngmo Feb 23 '21 at 15:58
  • RemoveAccount() ---->if(remove()). not sure what you meant. – Miraz Feb 23 '21 at 16:00
  • I mean that there are two different `remove` functions in the standard library and both remove files. There are actually three - but the third doesn't remove files. – Ted Lyngmo Feb 23 '21 at 16:03

1 Answers1

2

"jackson.txt" fits within an array of 12 chars. ID is an array of 10 chars. If you use that filename, you will access outside the bounds of the array and behaviour of the program is undefined.

Conclusion: Don't read unconstrained user input into constant size array. Also don't strcat unless you can prove that that the target array is sufficiently large. In fact, it may be better to not use strcat at all and instead use std::string which will make it more difficult for you to make incorrect assumptions.

eerorika
  • 181,943
  • 10
  • 144
  • 256
  • thanks brother! – Miraz Feb 23 '21 at 15:51
  • Being a newbie, I have trouble taking multi-word input using string variable. I used ```getline()```, but sometimes it skips a line if there is a space or newline in console. – Miraz Feb 23 '21 at 15:56
  • 1
    @Miraz read and understand this question: [https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – drescherjm Feb 23 '21 at 15:58