乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 25|回复: 8

[编程交流] findfile() vs open()

[复制链接]

218

主题

699

帖子

483

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1090
发表于 2022-7-5 16:10:55 | 显示全部楼层 |阅读模式
what is the different of
 
  1. (findfile filename)
 
vs
 
  1. (open filename)
回复

使用道具 举报

63

主题

6297

帖子

6283

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
358
发表于 2022-7-5 16:21:21 | 显示全部楼层
findfile function is to search for a specific file name that is existed or already created but open function would help you to open a specific file name in order to write or read from that file. eg: txt, csv, dat ... etc files but not all format of files.
 
Hope this helps.
回复

使用道具 举报

218

主题

699

帖子

483

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1090
发表于 2022-7-5 16:29:39 | 显示全部楼层
 
i dont undrestand what open() actually does?
 
findfile() already  returns the fullpath....so the open() return the same
 
confusing....
回复

使用道具 举报

63

主题

6297

帖子

6283

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
358
发表于 2022-7-5 16:33:44 | 显示全部楼层
 
It returns the file descriptor that allows you to write and read data from the return file name supported but the findfile function ensures if that file is existed otherwise it returns nil.
 
FINDFILE FUNCTION
 
OPEN FUNCTION
回复

使用道具 举报

218

主题

699

帖子

483

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1090
发表于 2022-7-5 16:46:18 | 显示全部楼层
 
Thanks Tharwat
 
1.what is descriptor ? can i tuch it?
2.findfile() will look in autocad library....does it mean in the supported path?
回复

使用道具 举报

63

主题

6297

帖子

6283

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
358
发表于 2022-7-5 16:51:05 | 显示全部楼层
You're welcome.
 
 
Suppose that you already have a txt file named: MyFile.txt then you need to read its contents, so when you use open function to open that file then the file in that case described or entitled as descriptor.
 
 
Findfile function can find any file located into your AutoCAD support folder if you feed the file name with path or not.
Eg:
 
Let's suppose that the MyFile.txt is already placed into you Support folder.
So the following should return the full path of the file.
 
  1. (findfile "MyFile.txt")
Also if you feed the findfile function with that MyFile.txt full path then it also should return the same return as the first one.
 
But if the MyFile.txt is not existed located into your Support Folder then you should feed the full path of any file to allow this function to search for that file.
 
If you read carefully the two links that I posted in my previous reply then you should be able to comprehend the two functions much better than anyone may describe. me included.
回复

使用道具 举报

218

主题

699

帖子

483

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1090
发表于 2022-7-5 16:57:11 | 显示全部楼层
THanks Tharwat
回复

使用道具 举报

63

主题

6297

帖子

6283

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
358
发表于 2022-7-5 17:03:46 | 显示全部楼层
 
No problem Shay.
 
Hope my description was good and clear to you!
 
Happy coding.
回复

使用道具 举报

66

主题

1552

帖子

1514

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
325
发表于 2022-7-5 17:16:16 | 显示全部楼层
Just leaving this for anyone learning:
 
  1. _$ (findfile "My_F2L2F_Example.txt")"C:\\Users\\Grrr\\desktop\\trusted acad path\\My_F2L2F_Example.txt" ; returns a string_$ (open "My_F2L2F_Example.txt" "r") ; open the file to readnil ; this failure means that we have to provide a path with filename and extension_$ (setq des (open "C:\\Users\\Grrr\\desktop\\trusted acad path\\My_F2L2F_Example.txt" "r")) ; open the file to read again# ; success_$ (type des)FILE ; this means that the type of the "descriptor" is known as symbol of 'FILE_$ (close des)nil
 
The following subfunctions might be handy for learning (regarding file-handling) :
  1. ; First create a ""My_F2L2F_Example.txt" file and place it within trusted acad path _$ (List->File "My_F2L2F_Example.txt" '("Hello," "It's me" "the Grrr guy" "from CADtutor.") )"C:\\Users\\Grrr\\desktop\\trusted acad path\\My_F2L2F_Example.txt"_$ (File->List "C:\\Users\\Grrr\\desktop\\trusted acad path\\My_F2L2F_Example.txt")("Hello," "It's me" "the Grrr guy" "from CADtutor.")_$ (File->List (List->File "My_F2L2F_Example.txt" '("This" "is" "entirely new" "content" "that has" "been writen" "and returned.") ))("This" "is" "entirely new" "content" "that has" "been writen" "and returned."); (List->File "My_F2L2F_Example.txt" '("Hello," "It's me" "the Grrr guy" "from CADtutor.") ); find_what - can be a filename with extension or a full filepath with filename and extension, i.e.: "Example.txt" or "C:\\Users\\Documents\\Example.txt"; L - list of strings(defun List->File ( find_what L / find desc ) (if   (and                                            ; wrap all evaluations, using (and) so the program will stop on the first evaluation that returned nil     L (listp L)                                   ; make sure that a list is provided, and its a list, since (listp nil) -> T     (vl-every '(lambda (x) (eq 'STR (type x))) L) ; make sure that every item inside the list is a string     (setq find (findfile find_what))              ; find the fine, should return the file's path as a string [sTR]     (setq desc (open find "w"))                   ; obtain file's descriptor (open for writing), using the file's path     (mapcar '(lambda (x) (write-line x desc)) L)  ; write each item of the list as a new line, inside the file     (and desc (not (close desc)))                 ; close the file's descriptor, (close desc) -> nil ; so we use: (not (close desc)) -> T   ); and   find                                            ; if everything is ok, return the file path we have found, else nil ); if ); defun List->File; (File->List "My_F2L2F_Example.txt"); find_what - can be a filename with extension or a full filepath with filename and extension, i.e.: "Example.txt" or "C:\\Users\\Documents\\Example.txt"(defun File->List ( find_what / find desc row L ) (and                                   ; wrap all evaluations, using (and) so the program will stop on the first evaluation that returned nil   (setq find (findfile find_what))     ; find the fine, should return the file's path as a string [sTR]   (setq desc (open find "r"))          ; obtain file's descriptor (open for reading), using the file's path   (progn                               ; wrap the following (while) loop inside a (progn), cause it would return nil     (while (setq row (read-line desc)) ; using a loop obtain the 'nth' row, until an end-of-line marker is encountered       (setq L (cons row L))            ; collect every row in a list     ); while      (and desc (not (close desc)))      ; close the file's descriptor, (close desc) -> nil ; so we use: (not (close desc)) -> T ; this also is used to return true for the (progn) expression   ); progn ); and (reverse L)                            ; reverse the list with collected rows, to return properly, (reverse nil) doesn't error out); defun File->List
 
I've wrote them as an example to samifox's questions, although would avoid the usage of (findfile) and rather expect a full path with filename and extension as an argument - because there are also functions like (getfiled) (vl-filename-mktemp)...
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-7-7 09:15 , Processed in 1.522020 second(s), 70 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表