ELisp: Open File, Read, Possibly Write

By Xah Lee. Date: . Last updated: .

Here's a idiom for efficiently opening a file, read content, and possibly write back to file.

(defun my-process-file (fPath)
  "Process the file at path FPATH"
  (let ((fileChanged-p nil))
    (with-temp-buffer
      (insert-file-contents fPath)

      ;; process text
      ;; set fileChanged-p to t or nil

      (when fileChanged-p (write-region (point-min) (point-max) fPath)))))

Emacs Lisp, File, Buffer