Problem
- Seems to be too obvious to advanced lisp programmers, but might be hard for beginners
- How can I write a simple string to a file
Solution
- (defun writeToFile (name content)
- (with-open-file (stream name :external-format charset:iso-8859-1
- :direction :output
- :if-exists :overwrite
- :if-does-not-exist :create )
- (format stream content))
- name)
Explanation
- content and name are both strings
- simply creates a file namend name if it does not already exist, if so it overwrites the file
- content is the input that is written to the file
- Internally: we are creating a stream object named name with certain parameters and in the body of this form we add the content to this stream via the format function
- For further parameters see the entry in the hyperspec open [1]
- This function returns the name of the file for further processing!
References
- [1] http://www.lisp.org/HyperSpec/Body/fun_open.html#open
- http://cl-cookbook.sourceforge.net/files.html gives first hints about reading a file
- http://cl-cookbook.sourceforge.net/io.html shows how to redirect *standard-output* to a file