如何在方案中将相同的显示(printf)写入文件?

时间:2012-06-06 06:39:21

标签: lisp scheme

使用TinyScheme。

我正在编写我的代码到文件(在这里解决了50%:How to write to a file in tinyscheme?):

(with-output-to-file "biophilia.c"
  (lambda ()
    (write code)
    ))
; and segmentation fault comes here

但它用“”qotes和\ n \ r \ n编写我的代码,因此它不会将其转换为换行符。

我需要编写与(display code)

相似的代码

racket docs的示例中有printf,但似乎TinyScheme实现没有printf,也许我需要发现(添加代码)printf?

2 个答案:

答案 0 :(得分:1)

你可以尝试:

(call-with-output-file "biophilia.c" 
    (lambda (port) 
        (write-string code port)))

提供“code”是一个字符串。它将删除任何转义,并将其写为纯文本。

答案 1 :(得分:0)

找到解决方案,唯一的解决办法就是将ex-hanged put-char写入write-char

  (define assert
    (lambda (aa msg)
      (if (null? aa)
    #t
    (if (not (car aa))
      (error msg)
      (assert (cdr aa) msg))))) 

 (display "define fprintf\n\r")
(define (fprintf port f . args)
   (let ((len (string-length f)))
     (let loop ((i 0) (args args))
       (cond ((= i len) (assert (null? args)))
         ((and (char=? (string-ref f i) #\~)
           (< (+ i 1) len))
          (dispatch-format (string-ref f (+ i 1)) port (car args))
          (loop (+ i 2) (cdr args)))
         (else
          (write-char (string-ref f i) port)
          (loop (+ i 1) args))))))

(display "define printf\n\r")
(define (printf f . args)
   (let ((port (current-output-port)))
     (apply fprintf port f args)
     (flush-output-port port)))

    (display "writing to output file biophilia.c\n\r")
    (with-output-to-file "biophilia.c"
      (lambda ()
        (printf code)
        ))

代码不再是段错误

但在文件的末尾:错误:(:25)没有足够的参数

相关问题