Как создать и записать в текстовый файл в Лисп (продолжение) - PullRequest
1 голос
/ 21 ноября 2019

https://stackoverflow.com/a/9495670/12407473

Из приведенного выше вопроса, когда я пытаюсь обработать код, я получаю

"Ошибка: нет определения функции: STR".

Может кто-нибудь сказать мне, почему это не работает для меня ?? Спасибо!

(with-open-file (str "/.../filename.txt"
                     :direction :output
                     :if-exists :supersede
                     :if-does-not-exist :create)
  (format str "write anything ~%"))

1 Ответ

0 голосов
/ 21 ноября 2019

Как отмечают другие в комментариях, пример кода, который вы используете, это Common LISP, а не AutoLISP (диалект LISP, используемый AutoCAD). Таким образом, такие функции, как str, with-open-file и format не определены в диалекте AutoLISP.

В 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
...