First of all, Im not too good at c++. so please take it easy on me.
I try to change the password but i only want to change a certain password in the text file.But when the text file got re-write like only one new password exits. the whole file got re-write.:(

void Passchange()
string tempass;
cout<<"enter current password: ";
cout<<"utility to change password stored in file\n";
cout<<"CHANGE PASS TO: ";
ofstream Passfile("user.txt", ios::out);
cout<<"password successfully changed";

Edited 4 Years Ago by mike_2000_17: Fixed formatting

If the text file contains many user names and passwords then you have to completly rewrite the entire file in order to change just one of their passwords.

Open the original file for reading
Open a new temp file for writing

For each line in the original file
   Read a line into memory
   if its the line for the user name you need to change then then the password in that string
   write the line out to the new temp file
end loop
close both files
delete the original file
rename temp file to the same name as the original

Another way to do that without using the temp file method described above is to read the entire file into a vector of strings, search the vector for the line that needs to be changed, then rewrite all the strings back out to the file.

Edited 6 Years Ago by Ancient Dragon: n/a

If you want to have multiple passwords stored in the file, you need you add the flag "ios::app" to the file output operation. To completely overwrite the file, then use the "ios::trunc" flag.

This article has been dead for over six months. Start a new discussion instead.