User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Nov 2007
Posts: 37
Reputation: picass0 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
picass0 picass0 is offline Offline
Light Poster

changing the expected row of text file

  #1  
Apr 23rd, 2008
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Posts: 4,596
Reputation: iamthwee is a jewel in the rough iamthwee is a jewel in the rough iamthwee is a jewel in the rough iamthwee is a jewel in the rough 
Rep Power: 15
Solved Threads: 294
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: changing the expected row of text file

  #2  
Apr 23rd, 2008
You have to delete the file then rewrite the file with the new password.
Member of: F-ugly code club

Join today don't delay!
Reply With Quote  
Join Date: Nov 2007
Posts: 37
Reputation: picass0 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
picass0 picass0 is offline Offline
Light Poster

Re: changing the expected row of text file

  #3  
Apr 23rd, 2008
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?
Reply With Quote  
Join Date: Aug 2005
Posts: 4,596
Reputation: iamthwee is a jewel in the rough iamthwee is a jewel in the rough iamthwee is a jewel in the rough iamthwee is a jewel in the rough 
Rep Power: 15
Solved Threads: 294
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: changing the expected row of text file

  #4  
Apr 23rd, 2008
>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.
Member of: F-ugly code club

Join today don't delay!
Reply With Quote  
Join Date: Nov 2007
Posts: 37
Reputation: picass0 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
picass0 picass0 is offline Offline
Light Poster

Re: changing the expected row of text file

  #5  
Apr 23rd, 2008
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?
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,199
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 34
Solved Threads: 824
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: changing the expected row of text file

  #6  
Apr 23rd, 2008
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]
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
Reply With Quote  
Join Date: Aug 2005
Posts: 4,596
Reputation: iamthwee is a jewel in the rough iamthwee is a jewel in the rough iamthwee is a jewel in the rough iamthwee is a jewel in the rough 
Rep Power: 15
Solved Threads: 294
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: changing the expected row of text file

  #7  
Apr 23rd, 2008
>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.
Member of: F-ugly code club

Join today don't delay!
Reply With Quote  
Join Date: Nov 2007
Posts: 37
Reputation: picass0 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
picass0 picass0 is offline Offline
Light Poster

Re: changing the expected row of text file

  #8  
Apr 23rd, 2008
Is there a way to get to the row that belong to marry?
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,199
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 34
Solved Threads: 824
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: changing the expected row of text file

  #9  
Apr 23rd, 2008
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
Reply With Quote  
Join Date: Nov 2007
Posts: 37
Reputation: picass0 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
picass0 picass0 is offline Offline
Light Poster

Re: changing the expected row of text file

  #10  
Apr 23rd, 2008
ok then can i cout the entire row that belong to marry?
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 2:33 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC