I'm trying to access a .txt file in read&write mode and then overwrite a specific portion of the text.
The .txt file stores a list of users, each one identified as such:
UserType:FirstName:LastName:Email:Password:endl:
The method in charge of making changes is tasked to parse the file and modify the email camp.
It takes in input an User object and the new email that has to be registered.
void userContainer::modifyEmail(User & u, string newmail){
fstream user_file;
string temp;
user_file.open("users.txt");
while(!(user_file.eof())){
getline(user_file,temp,':');
if(temp==u.getEmail()){
//save position of the email string
long pos=user_file.tellg();
//copy user data following the email
string remaining_data;
getline(user_file,remaining_data,'\n');
//overwrite with newmail
user_file.seekp(pos-(temp.size()+1));//positioning the pos value at the beginning of the email camp
user_file.write(&newmail[0],newmail.size()); //overwriting with the new mail
//writing remaining data;
user_file.write(&remaining_data[0], remaining_data.size());
}
}
user_file.close();
}
Upon running the method, no change whatsoever is applied to the users.txt file. The string "remaining_data" does, however, correctly register the strings following the email camp.
Deleting the portion of code which uses the remaining_data string does modify the file but, obviously, not in the way I want:
void userContainer::modifyEmail(User & u, string newmail){
fstream user_file;
string temp;
user_file.open("users.txt");
while(!(user_file.eof())){
getline(user_file,temp,':');
if(temp==u.getEmail()){
//save position of the email string
long pos=user_file.tellg();
//overwrite with newmail
user_file.seekp(pos-(temp.size()+1));
user_file.write(&newmail[0],newmail.size());
}
}
user_file.close();
}
Example:
Before modifyEmail is called:
admin:Cristiano:Ronaldo:cr7@fluffymail.com:cri7madr1d:endl:
After calling modifyEmail (without using remaining_data)
admin:Cristiano:Ronaldo:banana100000@hotmail.itmadr1d:endl:
Required behavior after calling the method
admin:Cristiano:Ronaldo:banana100000@hotmail:cri7madr1d:endl: