Let's say I have a text file that is 100 lines long. I want to only change what is in the 50th line.
One way to do it is open the file for input and open a new file for output. Use a for-loop to read in the first half of the file line by line and write to the second file line by line, then write what I want to change, and then write out the second half using a for-loop again. Finally, I rename the new file to overwrite the original file.
Is there another way besides this? A way to modify the contents in the middle of a file without touching the rest of the file and without writing everything out again?
If there is, then what's the code to do it?