Normal read/write "routine"
Posted on 2001-07-11
What is the standard "routine" for reading a file, modifying it, and saving it to disk again?
Currently, my simple app reads in a text file line by line. For each line, it simply does a writeln to a temporary output file. When the program sees the line that needs to be changed, it writes the correct line to the temp file instead. All the other lines in the file are then read and written to and from the original and temp files, respectively. When this is complete, both files are closed, the original is deleted, and the temp one gets the old file's name.
The problem is that the new file loses the old file's attributes, such as its creation date and possibly its permissions. What is the best way to make a change to a file, WITHOUT reading it all into one string, modifying the string, and writing the string back to the original?