1

I'm trying to create a change password function by searching for the username, if found confirm pass, if true then it replaces the password with newPass.

The file is written like so: USERNAME;PASSWORD

I'm using this to replace the string but not sure if its correct syntax (im new)

tempPass.replace(0, tempPass.length(), newPass); 

Here is my current code:

void AccountManager::changePassword(AccountManager & account) {
  string  username, password, newPass, passwordConf, tempUser, tempPass;
  fstream openFile("UserPass.txt", ios_base::out | ios_base::in | ios_base::app);

  // / Check if username exsists.
  do {
    cout << "Enter your username: " << endl;
    getline(cin, username);
    cout << "Enter you current password: " << endl;
    getline(cin, password);

    if (account.UserPass[username] != password) {
      cout << "Username and password do not match. " << endl;
    }
  } while (account.UserPass[username] != password);

  do {
    cout << "Enter new password: " << endl;
    getline(cin, newPass);
    cout << "Retype password: " << endl;
    getline(cin, passwordConf);

    if (newPass != passwordConf) {
      cout << "Password does not match confirmation. " << endl;
    }
  } while (newPass != passwordConf);

  // /find / replace password with newPass in file
  while (!openFile.eof()) {
    getline(openFile, tempUser, ';');
    getline(openFile, tempPass);

    if ((tempUser == username) && (tempPass == password)) {
      ofstream openFile("UserPass.txt", ios_base::app);

      tempPass.replace(0, tempPass.length(), newPass);    // changes pass in file at index ;+1
      cout << "Password has been changed. " << endl;
      switchLog(account);                                 // Login on successful password change.

      break;
    }
  }
  account.UserPass[username] = newPass;
}

Thank you,

user3646473
  • 33
  • 2
  • 9

1 Answers1

0

You can have a workaround by seeking to the position of the file and then replacing the data with exactly the same amount of bytes, however this not encouraged unless the file is very large and the replace happens for a very small amount of data.

The correct technique to edit the contents of a file is:

  1. Read the entire contents of the file
  2. Perform the modifications on the read data in memory
  3. Write this modified data to a new file
  4. Delete old file

Read Why is it not possible to erase contents from a file in C++? for more details.

Community
  • 1
  • 1
Cool_Coder
  • 4,888
  • 16
  • 57
  • 99
  • It is possible to write in any position of a file with an ostream. Think `seekp`. – Appleshell May 21 '14 at 03:36
  • Can you guide me in the direction to edit in append for my situation just for the sake of learning? as for making new file and copying everything over, how would I do that without breaking the entire program(swap out the names?) – user3646473 May 21 '14 at 05:01
  • Please take a look at the edited answer and mark it as correct if you feel it is helpful. – Cool_Coder May 21 '14 at 14:01
  • I need the syntax in my situation, not whether or not it can be done. I do appreciate your help though, ty. – user3646473 May 21 '14 at 22:44