Task

Remove a specific line or a number of lines from a file.

This should be implemented as a routine that takes three parameters (filename, starting line, and the number of lines to be removed).

For the purpose of this task, line numbers and the number of lines start at one, so to remove the first two lines from the file foobar.txt, the parameters should be: foobar.txt, 1, 2

Empty lines are considered and should still be counted, and if the specified line is empty, it should still be removed.

An appropriate message should appear if an attempt is made to remove lines beyond the end of the file.




procedure main() # remove lines
   removelines("foo.bar",3,2) | stop("Failed to remove lines.")
end
 
procedure removelines(fn,start,skip)
 
   f := open(fn,"r") | fail                  # open to read
   every put(F := [],|read(f))               # file to list
   close(f)
 
   if *F < start-1+skip then fail
   every F[start - 1 + (1 to skip)] := &null # mark delete
 
   f := open(fn,"w") | fail                  # open to rewrite
   every write(\!F)                          # write non-nulls
   close(f)
   return                                    # done
end