Dmitry Starostin
Dmitry Starostin

Reputation: 187

Read file by line and wait for keyboard input to read next line

A newbie question: (Common Lisp) read file by one line at a time and move to next by a keystroke.

There is a standard:

(let ((in (open "/some/file/name.txt" :if-does-not-exist nil)))
  (when in
    (loop for line = (read-line in nil)
        while line do (format t "~a~%" line))
    (close in)))

In the old Fortran I got accustomed to wait function which would somewhere in (when in wait 'action...).

There are solutions in Clojure by way of java.io.

But what about JUST Common Lisp?

Upvotes: 0

Views: 259

Answers (1)

sds
sds

Reputation: 60054

First of all, one should always use with-open-file instead of open/close.

Next, the ANSI CL standard does not specify i/o buffering, so there is no standard way to make CL react to each keystroke. You can, however, ask your user to hit Enter (or Return):

(with-open-file (in path)
  (loop for line = (read-line in nil nil)
    while line do
      (read-line)               ; wait for user to hit RET
      (format t "--> ~A~%" line)))

Upvotes: 6

Related Questions