[plt-scheme] writing to files

From: Marco Morazan (morazanm at gmail.com)
Date: Mon Jul 21 14:52:40 EDT 2008

Dear All,

Would someone be kind enough to point me in the right direction?

Consider the following code:

(define (w2file c filename)
  (let ((file (open-output-file
               (string-append
              "C:/Users/SHU-USER/Documents/Test/"
                 (symbol->string filename)
                 ".txt")
               #:mode 'text
               #:exists 'replace)))
    (begin
      (write2file c file)
      (close-output-port file)
    )))

(define (write2file c file)
  (begin
    (display c file)
    (display '#\newline file)))

Produces the following behavior:

Welcome to DrScheme, version 4.0.2 [3m].
Language: Essentials of Programming Languages (2nd ed.); memory limit:
128 megabytes.
> (w2file 'test 'test)
. . display: output port is closed
>

How is it that the output port is closed? If I comment out
(close-output-port file), no error is generated but the file created
is empty. Same results when using write instead of display.

Thanks,

Marco


Posted on the users mailing list.