[Gambas-user] Line Input: distinguish between Eof and empty line

Joel Martin github at martintribe.org
Wed May 9 16:19:05 CEST 2018


I'm working on an interactive console program. I'm trying to figure out how
to read one line at a time from stdin and exit when an EOF is sent (i.e.
Ctrl-D in Linux).

Check Eof() at the top of the loop doesn't work:

Dim inLine As String
While Not Eof()
  Line Input inLine
  Print "'" & inLine & "'"
Wend


The above ends up reading two lines at a time because Eof() waits for input.

I haven't found a way to distinguish Null from empty string so the
following doesn't work because it exits when an empty line is sent in
addition to exiting on Ctrl-D (I'm I'm not even sure whether Line Input is
returning a Null on EOF anyways):

Dim inLine As String
While True
  Line Input inLine
  If IsNull(inLine) Then
    Break
  EndIf
  Print "'" & inLine & "'"
Wend


It seems like checking the EndOfFile property on the stdin stream would be
an option except I don't know how to refer to the stdin stream directly by
name:

Dim inLine As String
While True
  Line Input inLine
  If Default.EndOfFile Then
    Break
  EndIf
  Print "'" & inLine & "'"
Wend


The above gives an error because of course Default is not recognized (it's
a keyword, not a stream). I've search the gambas source to see if there is
a name for the stdin stream but I haven't been able to find one.

What's the right way to do line-by-line input with proper detection of EOF
(vs empty line)?

Thanks,

Joel Martin (kanaka)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20180509/98ed5c9d/attachment.html>


More information about the User mailing list