How to edit text file?

May I know how to change a certain part of a text file? I want to +1 to a text file every time the user does a particular input. For example my text file has name << password << count . When the user logs in and choose option 1 the program will +1 to the count.

for e.g. Bob Bob123 1

when he logs in and chooses option 1.

it will change to Bob Bob123 2
As far as I know, there is no way to do this. The only way would be to load the whole file into memory, make the changes and then rewrite the file when you are done.
@ OP: You could play around with File Mapping and create an approximation of the behavior that you're describing but in reality the direct method is the best method. NT3's solution is the one I would go with. If you're trying to keep track of a users state by the way, that should be done in memory, hitting the HDD is always expensive compared to RAM. Remember that secondary storage should always be used as secondary storage.
Modifying a file in-place (such as with a File Mapping) assumes that the changes don't require insertions (or deletions), such as if "Bob Bob123 9" were to be changed to "Bob Bob123 10".

Load the file into memory, make your change, and write it out.

Beware, however, that Computergeek01 makes an important point -- if the file is large or the user chooses option 1 often a whole lot of times really fast (or both), then reading and writing a file (or even just a little-bitty piece of a file) is a very slow, expensive way to do it.

In that case, load the file into memory once at the beginning of your program, update the data in memory whenever something happens, then write the file back out before your program closes. (You can, of course, also choose other times to write the file back out to disk as appropriate.)

Hope this helps.
Topic archived. No new replies allowed.