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: Chet Ramey
Subject: Re: Prepending text to the beginning of a file
Date: Fri, 17 Jun 2022 10:01:16 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Thunderbird/91.10.0

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]