本文介绍了如何在方案中写入相同的显示(printf)文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用TinyScheme.

Using TinyScheme.

我正在将代码写入文件(在此处以50%的比例解决:如何使用tinyscheme写入文件?),方法如下:

I'm writing my code to file (solved it in 50% here: How to write to a file in tinyscheme?) with:

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

但是它按原样使用" qotes和\ n \ r编写我的代码,因此不会将其转换为换行符.

but it writes my code with "" qotes and \n\r as is so it doesn't translate it to newline.

我需要编写类似(display code)

,例如球拍文档有printf,但似乎TinyScheme实现没有printf,也许我需要发现(添加代码)printf ?

in example in racket docs there is printf but seems like TinyScheme implementation got no printf, maybe I need to discover (add code of it) printf?

推荐答案

找到解决方案,唯一的解决方法是将put-char挂到write-char

Found solution, the only fix is ex-hanged put-char to 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)参数不足

but in the end of file: Error: ( : 25) not enough arguments

这篇关于如何在方案中写入相同的显示(printf)文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 23:37