findfile() vs open()
what is the different of(findfile filename)
vs
(open filename) 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.
i dont undrestand what open() actually does?
findfile() alreadyreturns the fullpath....so the open() return the same
confusing....
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
Thanks Tharwat
1.what is descriptor ? can i tuch it?
2.findfile() will look in autocad library....does it mean in the supported path? 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.
(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. THanks Tharwat
No problem Shay.
Hope my description was good and clear to you!
Happy coding. Just leaving this for anyone learning:
_$ (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) :
; 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 (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 (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)...
页:
[1]