Input loop is part of Short Circuit's Console Program Basics selection.
Task

Read from a text stream either word-by-word or line-by-line until the stream runs out of data.

The stream will have an unknown amount of data on it.




CONST BUFLEN=1024, EOF=-1
 
PROC consume_input(fh)
  DEF buf[BUFLEN] : STRING, r
  REPEAT
    /* even if the line si longer than BUFLEN,
       ReadStr won't overflow; rather the line is
       "splitted" and the remaining part is read in
       the next ReadStr */
    r := ReadStr(fh, buf)
    IF buf[] OR (r <> EOF)
      -> do something
      WriteF('\s\n',buf)
    ENDIF
  UNTIL r=EOF
ENDPROC
 
PROC main()
  DEF fh
 
  fh := Open('basicinputloop.e', OLDFILE)
  IF fh
    consume_input(fh)
    Close(fh)
  ENDIF
ENDPROC