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.




"aFile.txt" var fileName
100 var startLine
10 var lineCount
0 var lineNum
 
fileName "r" fopen var in
fileName "_tmp" chain "w" fopen var out
 
in 0 > out 0 > and if
    true
    while
        in fgets dup
        -1 == if
            drop
            in fclose out fclose
            false
        else
            lineNum 1 + var lineNum
            lineNum startLine < lineNum startLine lineCount + >= or
            if out fputs else drop endif
            true        
        endif
    endwhile
    "del " fileName chain cmd drop
    "rename " fileName chain "_tmp " chain fileName chain cmd drop
else
    "WRONG! error in file operation" print
endif