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.




#DIM ALL
 
FUNCTION PBMAIN () AS LONG
    DIM filespec AS STRING, linein AS STRING
    DIM linecount AS LONG, L0 AS LONG, ok AS LONG
 
    filespec = DIR$(COMMAND$(1))
    WHILE LEN(filespec)
        linecount = 0: ok = 0
        OPEN filespec FOR INPUT AS 1
        OPEN filespec & ".tmp" FOR OUTPUT AS 2
        DO UNTIL EOF(1)
            LINE INPUT #1, linein
            INCR linecount
            IF VAL(COMMAND$(2)) <> linecount THEN
                PRINT #2, linein
            ELSE
                ok = -1
                FOR L0 = 2 TO VAL(COMMAND$(3))
                    IF EOF(1) THEN
                        ok = 1
                        PRINT "Tried to remove beyond the end of "; filespec
                        EXIT DO
                    END IF
                    LINE INPUT #1, linein
                NEXT
            END IF
        LOOP
        IF 0 = ok THEN
            PRINT "Lines to remove are beyond the end of "; filespec
        ELSEIF -1 = ok THEN
            PRINT filespec; ": done"
        END IF
        CLOSE
        filespec = DIR$
    WEND
END FUNCTION