i using seekp() function to change the pwd, but it can only change the first row of peter's pwd. Wat i wanted was to change only the pwd that belongs to only mary if marry has login. How can do it? need some advice!!!

this is wat my text file looks like:

peter:pwd:abcd
marry:pwd:efgh
jane:pwd:ijkl

ps: i try to match the username to my text file but it still only change the first row.

Recommended Answers

All 13 Replies

Member Avatar for iamthwee

You have to delete the file then rewrite the file with the new password.

if the file is deleted then the rest of the data will be gone also. i wanted to know how can i get to the row that below to marry?

Member Avatar for iamthwee

>if the file is deleted then the rest of the data will be gone also

So save the necessary info before you delete it and write it to another file.

thanks iamthwee!!! ur help is not i seeking for. As i m using seekg() function to get the column position of which i wanted and change the data. but i want to know how to get to the my text file marry row?

jamthwee is right -- in order to change something in a text file you have to rewrite the entire file, making whatever changes you want while writing the file. Calling seekg() does nothing to help you with that. If the current password is "abc" and the new password is "abcdefg" then you can't stuff 7 characters into the filespace for 3 characters.

There are at least two ways to do it:
1) read each line of the file into an array of lines, make in-memory change to marry's password, truncate the original file to 0 length, then finally rewrite all the lines back to the file.

2) create a new temp file -- then do this pseudocode

open original file for read
open new temp file for write
while not end of original file
    read a line from original file
    is it marry's line?
    yes, then make change to password
    write line to new temp file
end of while
close both files
delete original file
rename temp file to original file name
done

[edit]If the new password is exactly the same length as the password in the file, then open the file for read/write, seek to the position of the old password then just write in the new password.[/edit]

Member Avatar for iamthwee

>but i want to know how to get to the my text file marry row?

Pretty simple read in the file line by line.

Split each line by the : mark.

Use that to find the user.

Is there a way to get to the row that belong to marry?

Not directly, you have to read the file one line at a time until you find marry's row. When working with files with variable-length rows there is no easy way to do it.

ok then can i cout the entire row that belong to marry?

Huh?

i have this file which is like this:

marry:1234:id1
peter:4567:id2
wFile.open("passwd.txt",ios::in);
                        while(!rFile1.eof())
                        {
                              getline(rFile1,line);
                              
                              int row = line.length();
                              int nxtRow = row + 2;                   
                              
                              int found = line.find(":",0);
                              string u(line,0,found);
                              int len = u.length();
                              len = len+1;
                              int pos = nxtRow+len;
                              
                              wFile.seekp(pos);
                              wFile << NewPwd;
                              //wFile << s;
                              wFile.close(); 
                                              
                        }

this code able me to change the pwd of 2nd row in my text file. but if the user is peter it will change the pwd acorrding to the row of peter. Is there a way that will know if the user belongs to which row in my text file

i have this file which is like this:

marry:1234:id1
peter:4567:id2
wFile.open("passwd.txt",ios::in);
                        while(!rFile1.eof())
                        {
                              getline(rFile1,line);
                              
                              int row = line.length();
                              int nxtRow = row + 2;                   
                              
                              int found = line.find(":",0);
                              string u(line,0,found);
                              int len = u.length();
                              len = len+1;
                              int pos = nxtRow+len;
                              
                              wFile.seekp(pos);
                              wFile << NewPwd;
                              //wFile << s;
                              wFile.close(); 
                                              
                        }

this code able me to change the pwd of 2nd row in my text file. but if the user is peter it will change the pwd acorrding to the row of peter. Is there a way that will check that the user login belongs to which row in my text file

>>Is there a way that will check that the user login belongs to which row in my text file
probably, but it would be os dependent. Get the user's loging name using some system function call and then compare that with the user name on each line of the file. I have no idea how to get the login user name in *nix.

>> string u(line,0,found);
I don't think that works. What you want is this: string u = line.substr(0,found);

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.