views:

1043

answers:

2

I need to have a newline every time I write to a file in plt scheme. I wonder if there is a special procedure that allows me to do this.

+1  A: 

newline can take an optional argument of a port, on which it will emit a newline.

(define myport (open-output-file "greeting.txt"))
(display "hello world" myport)
(newline myport)
Jay Kominek
+1  A: 

If you're displaying a string as in Jay's example, you don't need to use newline -- MzScheme's strings include the usual C escapes, so you could just do

(with-output-to-file "foo.txt"
  (lambda ()
    (display "hello world\n")))

Note also that the with-... forms are generally better than in Jay's code, since that will require you to close the file explicitly -- MzScheme will not close a file that corresponds to a port that has been garbage-collected.

Eli Barzilay