Question

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.

Was it helpful?

Solution

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)

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top