From: Emanuel Berg via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org>
To: help-gnu-emacs@gnu.org
Cc: emacs-devel@gnu.org
Subject: read and write data to files
Date: Wed, 26 Feb 2020 23:04:20 +0100 [thread overview]
Message-ID: <87zhd5c8ez.fsf@ebih.ebihd> (raw)
Is this any good?
For the intended purpose, for once, performance
might be an issue and there will be many, many
reading and writings back and forth (sensors,
actions based on sensor readings, and so on in
a loop).
So optimizations are perhaps finally called
for, if anyone has any :)
;;; -*- lexical-binding: t -*-
;; this file: http://user.it.uu.se/~embe8573/emacs-init/file-write-to.el
;; https://dataswamp.org/~incal/emacs-init/file-write-to.el
;; write
(defun write-to-file (file string)
(write-region string nil file) )
;; read
(require 'subr-x)
(defun file-to-string (file)
"A string with the contents of FILE."
(interactive "Ffile: ")
(with-temp-buffer
(insert-file-contents file)
(string-trim
(buffer-substring-no-properties (point-min) (point-max)) )))
(defmacro file-to-string-variable (file var)
`(setq ,var ,(file-to-string file)) )
(defmacro file-to-integer-variable (file var)
`(setq ,var ,(string-to-number (file-to-string file))) )
;; (write-to-file "~/5ifth.txt" "Leeloo Dallas Multipass")
;; (write-to-file "~/element.txt" (number-to-string 5))
;;
;; (file-to-string "~/5ifth.txt") ; "Leeloo ..."
;; (file-to-string-variable "~/5ifth.txt" string-value) ; "Leeloo ..."
;; (file-to-integer-variable "~/element.txt" integer-value) ; 5
;;
;; string-value ; "Leeloo ..."
;; integer-value ; 5
(provide 'file-write-to)
--
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal
reply other threads:[~2020-02-26 22:04 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87zhd5c8ez.fsf@ebih.ebihd \
--to=help-gnu-emacs@gnu.org \
--cc=emacs-devel@gnu.org \
--cc=moasenwood@zoho.eu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).