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;
system("cls");
cout<<"enter current password: ";
cin>>tempass;
if(tempass==inpass)
{
cout<<"utility to change password stored in file\n";
cout<<"CHANGE PASS TO: ";
cin>>user;
ofstream Passfile("user.txt", ios::out);
Passfile<<user;
Passfile.close();
cout<<"password successfully changed";
getch();
main();
}

Recommended Answers

All 2 Replies

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.

Member Avatar for JSPMA1988

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.