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.


\util.g
proc nonrec main() void:
    /* first we need to declare a file buffer and an input channel */
    file() infile;
    channel input text in_ch;
 
    /* a buffer to store the line in is also handy */
    [256] char line;
    word counter;  /* to count the lines */
 
    /* open the file, and exit if it fails */
    if not open(in_ch, infile, "input.txt") then
        writeln("cannot open file");
        exit(1)
    fi;
 
    counter := 0;
 
    /* readln() reads a line and will return false once the end is reached */ 
    /* we pass in a pointer so it stores a C-style zero-terminated string,
     * rather than try to fill the entire array */
    while readln(in_ch; &line[0]) do
        counter := counter + 1;
        writeln(counter:5, ": ", &line[0])
    od;
 
    /* finally, close the file */
    close(in_ch)
corp