0

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.

Wasd
  • 31
  • 2

1 Answers1

1

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.

Chpock
  • 682
  • 3
  • 9
  • This is the sort of requirement where “use a database” is a correct response. (I'm guessing that PostgreSQL is the right sort of option.) – Donal Fellows Aug 05 '19 at 09:58