I tried to read string from an input-port in Racket, but no matter what API functions I used to read (read, read-string, read-bytes etc), the return value of those functions was never equal eof-object.
(define (some_process inp)
(begin
(let ([c (read-string 1 inp)])
(if (eof-object? c)
(begin
(display "EOF \n")
#f)
(if (equal? c "\n")
(begin
(display "NEWLINE \n"))
(some_process inp))))))
The c can never be an eof-object?
If you display what c is, it is always a newline.
Read reference:
read-char: “Reads a single character from
in– which may involve reading several bytes to UTF-8-decode them into a character. If no bytes are available before an end-of-file, theneofis returned.“read-string: “Returns a string containing the next
amtcharacters fromin. If no characters are available before an end-of-file, theneofis returned.“Examples:
But if there are no character(s) in the buffer, you’ll get
eof:I think you just want to read some amount of characters in a loop and do something with them. If so, the solution would look something along the lines of:
Example:
Notice a second call to
another-processwith an empty line, it detectseofimmediately and exits the loop.EDIT:
In case you need to check if the read character is newline:
Example:
Hope that helps.