reading lines from stream

Started by methodic, June 07, 2005, 03:10:57 PM

Previous topic - Next topic

methodic

just to clarify, a stream could be anything, all we can about is capturing on STDIN



Anyway, I have this code:



(do-until (= n "QUIT")

  (set 'n (read-line))

  (print n)

)



Basically everytime I read a line (termianted by CLRF), I want to be able to do something to what I just got (in this case, print it). I think this is a blocking issue, so is there a way to tell newlisp, globally, to do non-blocking on all IO operations?



Thanks.

Lutz

#1
'read-line' by definition blocks until it receives a line termination character. I guess you are using your program in a UNIX pipe?



I rearranged your program so it can handle the end of input from STDIN when 'read-line' returns 'nil'. And it will work just fine printing out every line until a line with 'quit' is encountered.



; pipedemo

(while (set 'n (read-line))
        (println n)
        (if (= n "QUIT") (exit)))


You could use this as a pipe:

newlisp pipdemo < sometext.txt

and it will print the lines in a shell windows until STDIN is exhausted or a line containing QUIT comes by.



Your code will do fine if there is a 'QUIT' line, but else, when used in a pipe will spew out 'nil' after 'nil when STDIN is exausted.



Lutz