help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: safe way to add contents to a file ?


From: Stefan Monnier
Subject: Re: safe way to add contents to a file ?
Date: Tue, 17 Dec 2019 22:59:15 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

> 1) if APPEND is a number, that's the position from which my contents will
> *overwrite* the rest of the file (I want to *insert* my contents)

This is the way life works, it's not specific to Emacs: given the way
files are typically represented on disk, there is simply no efficient
way to *insert* into a file.

> 2) there are weird things like the whole code where I call this function is
> actually copied to the target file buffer and I have no idea how to
> prevent that.

I don't understand what you're describing, really, but from where
I stand it sounds like you just had a weird bug in your code which made
it do something you did not intend.

> But I thought, it's not like such things don't happen all the time in emacs,
> so there must be a better workflow.

The normal workflow in Emacs is:
- read the file into a buffer.
- modify the buffer as you please (here, insertion is available and
  reasonably efficient).
- save the buffer back into the file.

The case you describe seems to fit this workflow perfectly and it'll be
pretty close to optimal compared to what could theoretically be obtained
from the POSIX API (my guess is that in the very worst case you'll be
performing twice the minimum number of disk accesses).

> But the reference is incredibly cryptic regarding that and the Intro does
> not address any file i/o at all...

Which part of which reference?  What have you looked for?

Given the structure of your problem, you have to read the file in order
to find the precise place where you want to insert the new content and
once you've read the file into a buffer, the rest seems to follow
quite naturally.


        Stefan




reply via email to

[Prev in Thread] Current Thread [Next in Thread]