-
Round-Robin File
Hi there!
What, in everyone's opinion, would be the best way to iteratively remove the top line of a file, move all the other lines up to fill the empty space, and then add another line at the bottom.
So there would be a file like
Value1 Value2 <------ delete this line
Value3 Value4
Value5 Value6
Value7 Value8
Value9 Value10 ^move all lines up one line
Value11 Value12
Value13 Value14
Value15 Value16
Value17 Value18 <------ add "Value 19 Value 20" in here
This way the size of the file remains fixed.
Any thoughts?
Thanks
-
Create a temporary file. Write all but the first line of the original file to it, append the new entry, delete the original file, and then rename the temp file to the original file name.
-
Since the file requires rewriting anyway, you could open it, and open a new tmp file, read the first line, then start copying one line at a time until you reach the end, then add a line. Then delete the old file and rename the new one.