help-bash
[Top][All Lists]
Advanced

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

Re: Prepending text to the beginning of a file


From: Akbarkhon Variskhanov
Subject: Re: Prepending text to the beginning of a file
Date: Fri, 17 Jun 2022 20:04:48 +0500

Now that is a great explanation! When you put it that way, the picture
becomes much clearer.

Thank you and have an awesome weekend!

On Fri, Jun 17, 2022 at 7:01 PM Chet Ramey <chet.ramey@case.edu> wrote:

> On 6/17/22 9:35 AM, Akbarkhon Variskhanov wrote:
> > In my understanding, it should let me, well, read from and write to the
> > file for in-place editing.
> >
> > Let's say that I open a file for both reading and writing:
> > exec 3<> file
> > Next, I'd like to write to it:
> > echo "some rather long text" >&3
> > At this point it's too soon for seeking and I expect this text to be
> > prepended to the beginning of the file, since I never explicitly stated
> to
> > begin reading from it like so:
>
> This is where your assumptions are wrong. The file has data and a file
> pointer (a single offset for both reading and writing), which is initially
> 0. You write to the file starting at that offset, overwriting the data that
> is there, advancing the file pointer to the end of the write. Then if you
> start reading, you get the data in the file at that offset, which is the
> old data.
>
> The invalid expectation is that writing to a file opens a `hole' and
> inserts the new text into it, rather than overwriting existing data.
>
> --
> ``The lyf so short, the craft so long to lerne.'' - Chaucer
>                  ``Ars longa, vita brevis'' - Hippocrates
> Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/
>


reply via email to

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