Elisp: How to save data in a file?

后端 未结 2 737
时光取名叫无心
时光取名叫无心 2021-02-04 07:43

I want to save data to a file in my elisp program. I have a multi-dimensional list that I want to save to a file, so I can restore it the next time my program runs. What\'s the

2条回答
  •  野的像风
    2021-02-04 08:26

    Another proposal. Instead of serialising setq calls, this one basically lets you use the file as a variable.

    (defun print-to-file (filename data)
      (with-temp-file filename
        (prin1 data (current-buffer))))
    
    (defun read-from-file (filename)
      (with-temp-buffer
        (insert-file-contents filename)
        (cl-assert (eq (point) (point-min)))
        (read (current-buffer))))
    

    Usage:

    (print-to-file "bla.el" '(1 2 "foo" 'bar))
    (1 2 "foo" (quote bar))
    
    (read-from-file "bla.el")
    (1 2 "foo" (quote bar))
    

提交回复
热议问题