[英]How to create and write into text file in Lisp (continued)
https://stackoverflow.com/a/9495670/12407473
從上面的問題,當我嘗試使用我得到的代碼時
“錯誤:沒有 function 定義:STR”。
誰能告訴我為什么它對我不起作用? 謝謝!
(with-open-file (str "/.../filename.txt"
:direction :output
:if-exists :supersede
:if-does-not-exist :create)
(format str "write anything ~%"))
正如評論中的其他人所指出的,您使用的示例代碼是 Common LISP,而不是 AutoLISP(AutoCAD 使用的 LISP 方言)。 因此,在 AutoLISP 方言中沒有定義str
、 with-open-file
和format
等函數。
在 AutoLISP 中,一般方法如下:
(if (setq des (open "C:\\YourFolder\\YourFile.txt" "w"))
(progn
(write-line "Your text string" des)
(close des)
)
)
評論,即:
;; If the following expression returns a non-nil value
(if
;; Assign the result of the following expression to the symbol 'des'
(setq des
;; Attempt to acquire a file descriptor for a file with the supplied
;; filepath. The open mode argument of "w" will automatically create
;; a new file if it doesn't exist, and will replace an existing file
;; if it does exist. If a file cannot be created or opened for writing,
;; open will return nil.
(open "C:\\YourFolder\\YourFile.txt" "w")
) ;; end setq
;; Here, 'progn' merely evaluates the following set of expressions and
;; returns the result of the last evaluated expression. This enables
;; us to pass a set of expressions as a single 'then' argument to the
;; if function.
(progn
;; Write the string "Your text string" to the file
;; The write-line function will automatically append a new-line
;; to the end of the string.
(write-line "Your text string" des)
;; Close the file descriptor
(close des)
) ;; end progn
;; Else the file could not be opened for writing
) ;; end if
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.