我有一个函数,它接受一个列表,然后提示用户输入文件名。此列表将写入默认路径,保存为文件名
类似(伪代码):
(define storeme
(lambda (lst)
(write-list lst filename default-path)))
不确定如何让它发挥作用。 “写入”只需要2个参数用于写入对象& [输出端口]。感谢
答案 0 :(得分:1)
你需要一个完整的路径名,假设你拥有它。将您的功能定义为:
(define store-me-at
(lambda (lst path)
(with-output-to-file path (lambda () (write lst)))))
使用with-output-to-file
时,current-output-port
已为您设置,write
不需要port
参数。
使用with-output-to-file
执行此操作可确保为port
打开的path
始终关闭,即使存在非本地退出(或涉及call/cc
的其他细微之处和相关的用途)。如果您只是使用open-output-file
然后使用close-ouput-port
,则无法获得任何此类保证。
答案 1 :(得分:0)
您应该将输出端口绑定到变量
像
这样的东西(define storeme
(lambda (lst)
(let* ((filename (prompt-for-filename)) ;;the prompt routine should retrun #f
(output ;for invalid filenames or user cancellations
(open-file-output-port ;;routine may vary be implementation and scheme revision
(string-append *default-path* filename))))
(begin (if filename
(begin (write lst output) (display-each "List Stored in " filename))
(display "List not stored"))
(close-output-port output)))))