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

Benoît Minisini g4mba5 at gmail.com
Wed May 9 16:27:49 CEST 2018


Le 09/05/2018 à 16:19, Joel Martin a écrit :
> 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)
> 

"stdin" is File.In, so you have to check File.In.EndOfFile

Regards,

-- 
Benoît Minisini


More information about the User mailing list