kass中lisp文件_LISP:如何从文件中读取内容并将其写入另一个文件?

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值