how to edit data in a file

last bit i need to finish a project is how to edit or update data in a file. is there any other way apart from reading all the data and rewriting into a new file?

well its a text file that holds usernames and passwords. i wanted to allow the user to change his or her password and that requires that the data stored in the file is changed. i just thought it wouldn't be feasible to read and rewrite the whole file to another file with a different name.
Last edited on
closed account (o3hC5Di1)
Hi there,

I think we would need a bit more specifics on what it is you are trying to do. For instance, editing an xml file would take a different approach from writing an ini file.

Possibly the answer to your question lies somewhere on this page:
http://cplusplus.com/reference/iostream/fstream/

Hope that helps.

All the best,

NwN
is there any other way apart from reading all the data and rewriting into a new file?


For simple data files, if the data to be replaced is exactly the same length as the data it is to be replaced with, you could do it in place, otherwise you need to read all of the data in and rewrite it. It's safest to write to a new file, delete the old file and rename the new file, but some choose to just truncate the old file and write the updated data back to it.
Topic archived. No new replies allowed.