Andrew1979 发表于 2022-7-5 22:10:12

创建文本文件

我不确定,但我认为这很简单,我想创建一个新的。来自lisp例程的txt文件。这能做到吗?我在互联网和这些论坛上搜索过,但找不到任何关于如何创建文件的信息。一切都只是告诉你如何写入现有文件
提前谢谢。

Lee Mac 发表于 2022-7-5 22:25:19

以最简单的形式:
(setq des (open "C:\\MyTextFile.txt" "w"))
(write-line "This is a test." des)
(close des)
 
还有一个更详细的例子:
(defun c:writetextfile ( / des txt ) ;; Define function, declare local variables
   (if (setq txt (getfiled "Create Text File" "" "txt" 1)) ;; Prompt user for filename/filepath
       (if (setq des (open txt "w")) ;; Attempt to create text file with given filename/filepath
         (progn ;; Evaluate the enclose expressions as the 'then' expression for the IF statement
               (write-line "This is a test." des) ;; Write line of text to text file
               (close des) ;; Close file descriptor
         ) ;; end PROGN
         (princ "\nUnable to create text file.") ;; Else the text file could not be created
       ) ;; end IF
       (princ "\n*Cancel*") ;; Else the user pressed Cancel
   ) ;; end IF
   (princ) ;; Suppress the return of the last evaluated expression
) ;; end DEFUN

Andrew1979 发表于 2022-7-5 22:48:35

好的,谢谢你。看起来我做得很好,但不起作用
 

(setq StrRx (open "C:\\WCcharset.txt" "w"))
(write-line p+ StrRx)
(close StrRx)

 
这是我的密码。如果我已经手动创建了文件,我的代码就可以工作,但一旦我从硬盘上删除了文件,它就不工作了,我会收到一个错误。
有什么想法吗?
我认为这可能与权限有关,但我确保我在c驱动器上的安全设置允许完全访问。
 
真的被难住了

Andrew1979 发表于 2022-7-5 22:57:02

好的,这是一个权限问题。无论我对c盘有多大的完全控制权,我仍然没有足够的权限创建文件,即使是手动创建。我喜欢窗户。

Lee Mac 发表于 2022-7-5 23:15:45

 
是的,如我上面的第二个示例所示,您需要包括错误捕获,以确保在尝试写入和关闭文件描述符之前返回有效的文件描述符,例如:
 
(if (setq des (open "C:\\test.txt" "w"))
   (progn
       (write-line "testing" des)
       (close des)
   )
   (princ "\nUnable to create/modify test.txt.")
)
页: [1]
查看完整版本: 创建文本文件