category
category

Reputation: 2155

bad input stream

I'm trying to pass some arguments into a new function that adds a column to a flat file. The call goes like:

(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") 

But SLIME returns the error:

#<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}>)

I was wondering, what am I missing here? The stream layout seems fine:

(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)))))))))                 

Upvotes: 0

Views: 270

Answers (2)

category
category

Reputation: 2155

Following the accepted answer, I replaced the do macro with dotimes as follows:

(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))))))))) 

Works like a charm.

Upvotes: 0

Rainer Joswig
Rainer Joswig

Reputation: 139251

You open a stream for output. But then you try to read from that output stream using READ-LINE. This does not work, naturally.

Upvotes: 2

Related Questions