Skip some lines in the beginning of file during loading file

Skip some lines in the beginning of file during loading file

Post by Elvis Nguy » Thu, 23 Jul 2009 22:29:43


Hi all,

I have the code snippset loading the test.dat file.

(defparameter *name* nil)
(defparameter *tokens* nil)
(defparameter *input* nil)
(defparameter *output* nil)

(defun load-file-meta-tool()
(with-open-file (stream "/home/test.dat" :direction :input)
(do ((line (read-line stream)
(read-line stream nil 'eof)))
((eq line 'eof) "Reached end of file.")

(setf line (refine-reaction line))
(print line)
(parse-reaction line)
(format t "~%Name: ~a~&- Input: ~s~&- Output: ~s~&" *name*
*input* *output*))))

But, I want to skip some stuffs in the beginning of the file. I only
want to handle the line while the line begins with the character 'R'.
How can we do that? How do we put in the code above?

Thanks a lot
 
 
 

Skip some lines in the beginning of file during loading file

Post by pjb » Thu, 23 Jul 2009 23:08:51

Elvis Nguyen < XXXX@XXXXX.COM > writes:


By just doing what you said. But of course, you would have first to
say it correctly. It's not "while", it's "when" the line begins with
'R' that you want to process it.




(defun load-file-meta-tool (path)
(with-open-file (stream path) ; do not hard code pathnames!
; :direction :intput is the default so...
(let ((data '()))
(do ((line (read-line stream nil 'eof) ; what if the file is empty?
(read-line stream nil 'eof)))
((eq line 'eof)
;; "Reached end of file." ; And you find that funny?
;; Return something useful!
data)
(when (and (plusp (length line)) (char= #\R (aref line 0)))
(let ((line (refine-reaction line))) ; do not use setf! let is more efficient.
;; DO NOT USE GLOBAL VARIABLES! values is more efficient!
(multiple-value-bind (name input output) (parse-reaction line)
(push (list name input output) data)
(format t "~%Name: ~a~&- Input: ~s~&- Output: ~s~&"
name input output))))))))

(load-file-meta-tool "/home/test.dat")


--
__Pascal Bourguignon__