How to resolve the algorithm Read a file line by line step by step in the Draco programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Read a file line by line step by step in the Draco programming language

Table of Contents

Problem Statement

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

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Read a file line by line step by step in the Draco programming language

Source code in the draco programming language

\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

  

You may also check:How to resolve the algorithm Phrase reversals step by step in the Fortran programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the ZX Spectrum Basic programming language
You may also check:How to resolve the algorithm Musical scale step by step in the RPL programming language
You may also check:How to resolve the algorithm Map range step by step in the COBOL programming language
You may also check:How to resolve the algorithm Legendre prime counting function step by step in the jq programming language