Change content/line of text in a File

Hello, I want to change a line of text/content in a text file. There are many options, like copy the entire content of the file to a temporary file, seek the content you want and then delete it, put another content in it, close the temporary file and copy everything back. But is there not a easier way to do this?

NOTE: I am dutch so if there are words in the code that you don't understand, then it's probarly dutch and I didn't change it to English.
Also the text formatting of the code is completely gone, but that's not my fault.
Thanks

Hello, I want to change a line of text/content in a text file. There are many options, like copy the entire content of the file to a temporary file, seek the content you want and then delete it, put another content in it, close the temporary file and copy everything back. But is there not a easier way to do this?

The method you describe is not workable. You have the same problem updating the temporary file as you do with the original file. The usual method to make changes is to read in all of the file data, make any changes to the data and write all of the data back to the file.

This can be accomplished in several ways, the simplest (and least safe is to:)

* Open the data file
* Read the data in
* Close the data file
* Make changes to data
* Reopen the file for output, truncating the file
* Write all of the data back to the file
* Close the file

This can be avoided if the data to be replaced is exactly the same length as the data it is being replaced with. There are ways around this, of course, but they're likely to be overkill for your use case -- you'd be much better off using a database of some sort rather than reinventing one.