糟糕的输入流

时间:2013-02-19 16:13:40

标签: io stream common-lisp

我正在尝试将一些参数传递给一个向平面文件添加列的新函数。电话如下:

(add-col-to-ff '(:absolute "home" "lambda" "Documents") "test-col-col"
               '(:absolute "home" "lambda" "Documents") "test-col-ff"     
               '(:absolute "home" "lambda" "Documents") "test-col-new.txt") 

但是SLIME返回错误:

#<SB-SYS:FD-STREAM for "file /home/lambda/Documents/test-col-new.txt" 
{B50D7E1}> is not a character input stream.
[Condition of type SIMPLE-TYPE-ERROR]
Backtrace:
  0: (SB-KERNEL:ILL-IN #<SB-SYS:FD-STREAM for "file /home/lambda/Documents/test-    
     col-new.txt" {B50D7E1}>)

我在想,我在这里错过了什么?流布局似乎很好:

(defun add-col-to-ff (col-dir col-file 
                      ff-dir ff-file 
                      out-dir out-file)
  (let ((M (make-ff-array ff-dir ff-file))
        (vec (vec-from-1col-ff col-dir col-file))
        (path (make-pathname :name out-file 
                             :directory out-dir)))
    (with-open-file (addcol-str path :direction :output)
      (do ((line (read-line addcol-str nil 'eof)
                 (read-line addcol-str nil 'eof))
           (i 0 (1+ i)))
          ((eql i (array-dimension M 1))) 
          (dotimes (k (array-dimension M 0))
            (cond ((eql (1+ k) (array-dimension M 0))
                   (format addcol-str "~A ~A~%" (aref M i k) (svref vec k)))
                  ((and (eql (1+ i) (array-dimension M 1)) 
                        (eql (1+ k) (array-dimension M 0)))
                   (format addcol-str "~A ~A"  (aref M i k) (svref vec k)))
                  (t (format addcol-str "~A " (aref M i k)))))))))                 

2 个答案:

答案 0 :(得分:2)

您打开输出的流。但是,您尝试使用READ-LINE从该输出流读取。这自然不起作用。

答案 1 :(得分:0)

根据接受的答案,我将do宏替换为dotimes,如下所示:

(defun add-col-to-ff (col-dir col-file 
                      ff-dir ff-file 
                      out-dir out-file)
  (let ((M (make-ff-array ff-dir ff-file))
        (vec (vec-from-1col-ff col-dir col-file))
        (path (make-pathname :name out-file 
                             :directory out-dir)))
    (with-open-file (addcol-str path :direction :output)
      (dotimes (i (array-dimension M 0)) 
          (dotimes (k (array-dimension M 1))
            (cond ((eql (1+ k) (array-dimension M 0))
                   (format addcol-str "~A ~A~%" (aref M i k) (svref vec k)))
                  ((and (eql (1+ i) (array-dimension M 0)) 
                        (eql (1+ k) (array-dimension M 1)))
                   (format addcol-str "~A ~A"  (aref M i k) (svref vec k)))
                  (t (format addcol-str "~A " (aref M i k))))))))) 

像魅力一样。