Read a file one line at a time,
as opposed to reading the entire file at once.



Related tasks

 Read a file character by character
 Input loop.


start_up = proc ()
    po: stream := stream$primary_output()
 
    % There is a special type for file names. This ensures that
    % the path is valid; if not, file_name$parse would throw an
    % exception (which we are just ignoring here).
    fname: file_name := file_name$parse("input.txt")
 
    % File I/O is then done through a stream just like any I/O.
    % If the file were not accessible, stream$open would throw an
    % exception. 
    fstream: stream := stream$open(fname, "read")
 
    count: int := 0  % count the lines
    while true do
        % Read a line. This will end the loop once the end is reached,
        % as the exception handler is outside the loop.
        line: string := stream$getl(fstream)
 
        % Show the line
        count := count + 1
        stream$putl(po, int$unparse(count) || ": " || line)
    end except when end_of_file:
        % Close the file once we're done
        stream$close(fstream)
    end
end start_up