•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 375,199 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,127 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 374 | Replies: 13
![]() |
•
•
Join Date: Nov 2007
Posts: 37
Reputation:
Rep Power: 1
Solved Threads: 0
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:
ps: i try to match the username to my text file but it still only change the first row.
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.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,199
Reputation:
Rep Power: 34
Solved Threads: 824
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
[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]
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]
Last edited by Ancient Dragon : Apr 23rd, 2008 at 9:02 am.
'Politics' is made up of two words, 'poli,' which is Greek for 'many,' and 'tics,' which are blood-sucking insects.
- Gore Vidal
Being ignorant is not so much a shame as being unwilling to learn. - Benjamin Franklin
- Gore Vidal
Being ignorant is not so much a shame as being unwilling to learn. - Benjamin Franklin
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,199
Reputation:
Rep Power: 34
Solved Threads: 824
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.
'Politics' is made up of two words, 'poli,' which is Greek for 'many,' and 'tics,' which are blood-sucking insects.
- Gore Vidal
Being ignorant is not so much a shame as being unwilling to learn. - Benjamin Franklin
- Gore Vidal
Being ignorant is not so much a shame as being unwilling to learn. - Benjamin Franklin
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Similar Threads
- Python GUI Problem (Python)
Other Threads in the C++ Forum
- Previous Thread: developers using eclipse IDE for C++ development?
- Next Thread: C++ Complex Matrix Library....



Linear Mode