Jump to content

Read & Write txt file


jonathann3891

Recommended Posts

I'm learning how to have a lisp read & write information to a text file. I am a little stuck and looking for some help.

 

Everything seems to be working up to the point where I try to set the OSMODE to USNAPS.

All it says is "Insufficient Data". Why is this different from saving the osmode setting then recalling it later?

 

Please critique my code as well.  Is there a better way to write this?

(defun c:ccc (/ Usnaps Uconfig)
  (setq Usnaps (rtos (getvar 'osmode) 2 0))
  (if (setq Uconfig (open "C:\\Users\\nortonj1\\Documents\\AutoCAD\\Support\\UserConfig.txt" "w"))
    (progn
      (write-line (strcat Usnaps) Uconfig)
      (close Uconfig)
      )
    )
  (princ)
  )


(defun c:ttt (/)
  (setq fname (open "C:\\Users\\nortonj1\\Documents\\AutoCAD\\Support\\UserConfig.txt" "r"))
  (if (not fname)
    (prompt "File not found! ")
    )
  (progn (setq Usnaps (read-line fname))(close fname))
  (setvar 'osmode Usnaps)
  )

 

Link to comment
Share on other sites

Hi,

Based on your way of coding I did modify the codes a bit but untested:

(if (not (setq fname (open "C:\\Users\\nortonj1\\Documents\\AutoCAD\\Support\\UserConfig.txt" "r")))
  (prompt "File not found! ")
  (progn (setq Usnaps (read-line fname))
         (close fname)
         (setvar 'osmode (atoi Usnaps))
  )
)

 

Link to comment
Share on other sites

Thanks @Tharwat!

 

This is something I pieced together by looking at other examples. If there is a better way to code it, I would like to know. 

Link to comment
Share on other sites

Quickly written and untested:

(defun c:wrt (/ find file)
  (if (setq file (open "C:\\Users\\nortonj1\\Documents\\AutoCAD\\Support\\UserConfig.txt" "w"))
    (progn (write-line (itoa (getvar 'osmode)) file)
           (close file)
    )
  )
  (princ)
)

;;				;;

(defun c:red (/ find file val)
  (if (setq find (findfile "C:\\Users\\nortonj1\\Documents\\AutoCAD\\Support\\UserConfig.txt"))
    (progn (setq file (open find "r"))
           (setq val (read-line file))
           (close file)
           (setvar 'osmode (atoi val))
    )
    (alert "\nFile not found !")
  )
  (princ)
)

 

Edited by Tharwat
Link to comment
Share on other sites

The reason that your original code fails is because the content read from your file will be a string, and the OSMODE system variable requires an integer value.

 

Since you are looking for suggestions, I would recommend the following code:

(setq snap:file "C:\\Users\\nortonj1\\Documents\\AutoCAD\\Support\\UserConfig.txt")

(defun c:writesnaps ( / des )
    (if (setq des (open snap:file "w"))
        (progn
            (princ (getvar 'osmode) des)
            (close des)
            (princ "\nObject Snap Mode written to file.")
        )
        (princ (strcat "\nUnable to create or write to file: \"" snap:file "\"."))
    )
    (princ)
)
(defun c:readsnaps ( / des )
    (if (setq des (open snap:file "r"))
        (progn
            (setvar 'osmode (atoi (read-line des)))
            (close des)
            (princ "\nObject Snap Mode restored from file.")
        )
        (princ (strcat "\nThe file \"" snap:file "\" was not found or could not be read."))
    )
    (princ)
)

Here, the location of the file is defined as a global variable which is referenced in both functions - this approach has the benefit that any change to the location or name of the file can be made in a single place in the code and will be automatically reflected in both functions.

 

Also note the use of the atoi function to convert the string value to an integer (converting to 0 in the worst case) before updating the OSMODE system variable value.

Link to comment
Share on other sites

On 4/24/2019 at 2:36 PM, Tharwat said:

Quickly written and untested

 

Note that you are writing the file in both functions; also, the test for the existence of the file in the first function is unnecessary, since the write mode of the open function will create the file if it doesn't exist, else it will overwrite the file if it does.

Link to comment
Share on other sites

Maybe something like this also can be used in lisp code just needs to be loaded on startup. To use (c:47) is all required likewise (c:0) to set no snaps.

 


(defun C:15 ()(setvar "osmode" 15359))   ; sets all snaps on
(defun C:47 ()(setvar "osmode" 47))
(defun C:99 ()(setvar "osmode" 99))
(defun C:8 ()(setvar "osmode" 8))
(defun C:512 ()(setvar "osmode" 512))
(defun C:9 ()(setvar "osmode" 9))

(defun C:0 ()(setvar "osmode" 0))

 

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...