spk
spk

Reputation: 173

Write data to a non existing file in scheme (after creating through the program)

I have made a simple program to write text to existing files:

;; write to an existing file

(define write-to-a-file
  (lambda (path txt)
    (call-with-output-file path
      (lambda (output-port)
        (write txt output-port)))))

But I want to modify it so that, if the file does not exist, it should be created. If the file exists, then the it should write to the file without deleting previous content of the file. I'm writing in chicken scheme. Any ideas?

Upvotes: 2

Views: 644

Answers (1)

Óscar López
Óscar López

Reputation: 235994

Try this for Chicken Scheme:

(define (write-to-a-file path txt)
  (call-with-output-file path
    (lambda (output-port)
      (write txt output-port))
    #:append))

Or this for Racket:

(define (write-to-a-file path txt)
  (call-with-output-file path
    (lambda (output-port)
      (write txt output-port))
    #:exists 'append))

Upvotes: 6

Related Questions