也是别人的函数,具体是谁分享的也忘记了都是拿来主义
- ;; 按行读文本文件到表 (快于 read-line 函数)
- (defun VlDos_ReadFile (Fil / FilObj FilPth FilSys OpnFil All)
- (if (and (setq FilPth (findfile Fil)) (setq FilSys (vlax-create-object "Scripting.FileSystemObject")))
- (progn
- (setq FilObj (vlax-invoke FilSys 'GetFile FilPth)
- OpnFil (vlax-invoke FilObj 'OpenAsTextStream 1 0) ;只读模式
- All (car (Cov-Str2List (vlax-invoke OpnFil 'readall) '("\r\n"))) ;字符串转表
- )
- (vlax-invoke OpnFil 'Close)
- (vlax-release-object OpnFil)
- (vlax-release-object FilObj)
- (vlax-release-object FilSys)
- )
- )
- All
- )
- ;; 将字符串或表写入文件 (快于 write-line 函数)
- (defun VlDos_WriteFile (Fil TXT Mode / FilObj FilPth FilSys OpnFil Line)
- (if TXT
- (progn
- (if (and (member mode '("w" "W")) (findfile Fil))
- (vl-file-delete (findfile Fil)) ;为T时删除原文件
- )
- (if (setq FilSys (vlax-create-object "Scripting.FileSystemObject"))
- (progn
- (if (null (setq FilPth (findfile Fil)))
- (setq OpnFil (vlax-invoke FilSys 'CreateTextFile Fil 0 0)) ;文件编码为为ASCII格式(最后一个参数控制编码 0 = ascii编码, -1 = unicode编码, -2 system default编码)
- (setq FilObj (vlax-invoke FilSys 'GetFile FilPth)
- OpnFil (vlax-invoke FilObj 'OpenAsTextStream 8 0) ;添加模式
- )
- )
- (if OpnFil
- (progn
- (vlax-invoke OpnFil 'Write (strcat (Cov-All_To_EleStr TXT "\r\n") "\r\n")) ;尾部加换行,不然添加模式下会有重叠的行
- (vlax-invoke OpnFil 'Close)
- (vlax-release-object OpnFil)
- (if (= (type FilObj) 'vla-object)
- (vlax-release-object FilObj)
- )
- (vlax-release-object FilSys)
- )
- )
- )
- )
- (if (setq Filpth (findfile Fil))
- (setq FilPth (YX:Get-VlDos_FormatPath filpth))
- )
- )
- )
- filpth
- )
|