write to a non existing file

Discussion of Scheme and Racket

write to a non existing file

Postby sepuku » Thu May 03, 2012 4:47 pm

I have written a version to write to an existing file:

Code: Select all
;; 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)))))

I want to implement a version that checks if the file exists then the text is added to the file without deleting the previous content. If it does not exist then it should just create it and write text to it.
I 'm writting in chicken scheme. I have chicken wiki and the api page but the just mention the commands without some simple examples to get you started. :(
Any ideas?
Posts: 12
Joined: Thu May 12, 2011 11:00 am

Re: write to a non existing file

Postby nuntius » Fri May 04, 2012 2:40 pm

The behavior you describe is often phrased something as "open the file in append mode". Other flags are sometimes needed.
For example, in CL the call is "(open filename :if-exists :append :if-does-not-exist :create)".
In C, the call is "open(filename, O_APPEND|O_CREAT)".
The following page shows the options for several scheme implementations. http://practical-scheme.net/wiliki/sche ... utput-file
User avatar
Posts: 522
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Return to Scheme

Who is online

Users browsing this forum: No registered users and 1 guest