I want to write a function that has as arguments the names of the two files and copies the content from the first file to the second one.
So far I wrote a function that reads from a file:
(defun readFile (name)
(let ((in (open name)))
(format t "~a~%" (read-line in))
(close in)))
And a function that writes a string to a file:
(defun writeFile (name content)
(with-open-file (stream name
:direction :output
:if-exists :overwrite
:if-does-not-exist :create)
(format stream content)))
Following Savantes instructions I wrote the function again and this is how it looks:
(defun read-write-to-file (input-file output-file)
(WITH-OPEN-FILE (output-stream output-file
:direction :output
:if-exists :new-version
:if-does-not-exist :create)
(WITH-OPEN-FILE (input-stream input-file
:direction :input)
(FORMAT output-stream "~a" (READ input-stream nil 'eof))
)))
Now the only problem is that it doesn't read the entire file.
解决方案
You found with-open-file. Use it for input and output. Don't use open and close instead.
Open both files, then write to the output stream what you read-line from the input stream.
Your WRITEFILE obviously does not compile, by the way.
Also, please use proper names and indentation.
EDIT after question update:
Read-line reads one line. You can write it with write-line, so that your line endings are kept. You need to do that in a loop in order to copy more lines.
Hint: You should write your names in lower case in the code. The reader automatically upcases them. I chose to write WRITEFILE above to show how your chosen name is treated internally. Capitalization is not relevant. L and l are the same. Parts of names are conventionally separated by hyphens in Lisp code.