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.




$ include "seed7_05.s7i";
  include "osfiles.s7i";
 
const proc: removeLines (in string: fileName, in integer: start, in integer: count) is func
  local
    var file: inFile is STD_NULL;
    var file: outFile is STD_NULL;
    var integer: lineNumber is 1;
    var string: line is "";
  begin
    inFile := open(fileName, "r");
    outFile := open(fileName & ".tmp", "w");
    while hasNext(inFile) do
      line := getln(inFile);
      if lineNumber < start or lineNumber >= start + count then
        writeln(outFile, line);
      end if;
      incr(lineNumber);
    end while;
    close(inFile);
    close(outFile);
    removeFile(fileName);
    moveFile(fileName & ".tmp", fileName);
  end func;
 
const proc: main is func
  begin
    if length(argv(PROGRAM)) = 3 then
      removeLines(argv(PROGRAM)[1], integer parse (argv(PROGRAM)[2]), integer parse (argv(PROGRAM)[3]));
    end if;
  end func;