From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: Emanuel Berg via Users list for the GNU Emacs text editor Newsgroups: gmane.emacs.help,gmane.emacs.devel Subject: read and write data to files Date: Wed, 26 Feb 2020 23:04:20 +0100 Message-ID: <87zhd5c8ez.fsf@ebih.ebihd> Reply-To: Emanuel Berg Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="120893"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) Cc: emacs-devel@gnu.org To: help-gnu-emacs@gnu.org Cancel-Lock: sha1:1VnjyBJ/aq0nxZgNjHiSqCBRBDw= Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Wed Feb 26 23:05:10 2020 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1j74nm-000VMc-RV for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 26 Feb 2020 23:05:10 +0100 Original-Received: from localhost ([::1]:50726 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j74nl-0002Zj-Su for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 26 Feb 2020 17:05:09 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:49975) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j74n7-0002Ll-Ia for help-gnu-emacs@gnu.org; Wed, 26 Feb 2020 17:04:30 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j74n6-0001Kf-CC for help-gnu-emacs@gnu.org; Wed, 26 Feb 2020 17:04:29 -0500 Original-Received: from ciao.gmane.io ([159.69.161.202]:57430) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j74n6-0001Ia-5f for help-gnu-emacs@gnu.org; Wed, 26 Feb 2020 17:04:28 -0500 Original-Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1j74n3-000URu-Sk for help-gnu-emacs@gnu.org; Wed, 26 Feb 2020 23:04:25 +0100 X-Injected-Via-Gmane: http://gmane.org/ Mail-Followup-To: help-gnu-emacs@gnu.org Original-Followup-To: gmane.emacs.help Mail-Copies-To: never X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 159.69.161.202 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:122488 gmane.emacs.devel:245084 Archived-At: 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