In TCL, is it possible to delete top few or n lines of file, when file is under continuous write operation

150 Views Asked by At

Below is the piece of code for how the data is being written into file.

set fid [open "file.txt" w]
fconfigure $fid -buffering line

I would like to know if there is any way to delete top n lines of the file without closing fileID i.e., fid, while the file is in writing mode.

1

There are 1 best solutions below

1
Chpock On

It's impossible to do because of the way the files are handled. It's also unrelated to the Tcl/Tk language, it's impossible with any programming language.

To solve this problem, some workarounds are used.

For example, 1) read file in memory; 2) remove unwanted data from the beginning 2) write the data back to file.

Or: 1) open original file for reading 2) open temporary file for writing 3) skip unwanted data from the original file 4) read needed data from the original file and write it to the temporary file 5) close the original file and the temporary file 6) delete the original file and rename the temporary file.