|
Home > Archive > Unix Programming > July 2004 > Writing to beginning of file?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Writing to beginning of file?
|
|
| Bigdakine 2004-07-28, 6:19 pm |
| Is there a way in C to add records to the beginning of file, as opposed to the
end?
Sure there is the sledge hammer approach,
where I could write the new record to a file and
system( "cat new_record file > temp; mv temp file")
Is there something more elegant? I though about opening the file and doing a
seek(0), but I figure all that will do then is overwrite the first x number of
bytes.
Thanx in advance,
Stuart
| |
| Jens.Toerring@physik.fu-berlin.de 2004-07-28, 6:19 pm |
| Bigdakine <bigdakine@aol.comgetagrip> wrote:
> Is there a way in C to add records to the beginning of file, as opposed to the
> end?
> Sure there is the sledge hammer approach,
> where I could write the new record to a file and
> system( "cat new_record file > temp; mv temp file")
> Is there something more elegant? I though about opening the file and doing a
> seek(0), but I figure all that will do then is overwrite the first x number
> of bytes.
No, there isn't any other approach than rewriting the file. The only
thing you could do without using a temporary file is moving every-
thing in the file to some location more to at the end and then over-
writing the stuff at the start. But I guess that would be a lot
slower.
Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.toerring.de
| |
| Rich Teer 2004-07-28, 6:19 pm |
| On Wed, 28 Jul 2004, Bigdakine wrote:
> Is there a way in C to add records to the beginning of file, as opposed to the
> end?
>
> Sure there is the sledge hammer approach,
>
> where I could write the new record to a file and
>
> system( "cat new_record file > temp; mv temp file")
>
> Is there something more elegant? I though about opening the file and doing a
> seek(0), but I figure all that will do then is overwrite the first x number of
> bytes.
Something cunning involving sendfilev() and rename() comes to mind,
but it doesn't avoid the temporary file problem.
--
Rich Teer, SCNA, SCSA
President,
Rite Online Inc.
Voice: +1 (250) 979-1638
URL: http://www.rite-online.net
| |
| Pascal Bourguignon 2004-07-28, 6:19 pm |
| Rich Teer <rich.teer@rite-group.com> writes:
> On Wed, 28 Jul 2004, Bigdakine wrote:
>
>
> Something cunning involving sendfilev() and rename() comes to mind,
> but it doesn't avoid the temporary file problem.
Because this is not a problem of C, shell or language in general, but
a problem of file system. There is no notion of negative file
position, so you cannot write before file position 0.
If you had been more far-sighted, you could have started to write in
your file for example at position start=0x1000000, and now you could
just seek at some position start-size to write size byte.
Of course, all the programs that use this file would have to know
where the current start of file is. A convenient way to do it, is to
add a header at the physical start of the file indicating the current
virtual start of file.
Once you've done that, and given you're telling us that you've got
"records", why not forget about the sequential files and just use an
indexed file where you can insert and remove records at any position.
Check db(3). btree(3), hash(3), recno(3), etc.
Remember, the unix system is simple. That is, IT is simple. It leave
the complexities up to the applications or the libraries used by the
applications. So you HAVE to use a more sophisticated file access
method library if you want to do any fancy thing with your files.
Now, for the temporary file, this is NOT a problem. At least it's
much less a problem than a botched up file that could occur if you
implemented such a scheme:
You have to insert size byte at the beginning of the file.
Allocate a second buffer size byte long.
Read into this second buffer the beginning of the file.
Write the first buffer (the bytes to insert) at the beginning of the file.
Swap the buffers and start again size byte further in the file.
Stop when you've read and written the last buffer in the file.
Imagine what happens if the process crashes in the middle of such an insertion!
It is much safer to do: cat insert file > file.new && mv file.new file
--
__Pascal Bourguignon__ http://www.informatimago.com/
There is no worse tyranny than to force a man to pay for what he does not
want merely because you think it would be good for him. -- Robert Heinlein
| |
| Pascal Bourguignon 2004-07-28, 6:19 pm |
|
"Dan Mercer" <dmercer@mn.rr.com> writes:
> "Bigdakine" <bigdakine@aol.comGetaGrip> wrote in message news:20040728052202.15071.00000585@mb-m02.aol.com...
> : Is there a way in C to add records to the beginning of file, as opposed to the
> : end?
> :
> : Sure there is the sledge hammer approach,
> :
> : where I could write the new record to a file and
> :
> : system( "cat new_record file > temp; mv temp file")
>
> You could also:
>
> system("exec 3<file;rm -f file;cat new_file - <&3 >file");
With interesting results when the cat process crashes in the middle of
copying...
> Or you could write your own routine, but it's very unlikely to be much
> more efficient than cat.
--
__Pascal Bourguignon__ http://www.informatimago.com/
There is no worse tyranny than to force a man to pay for what he does not
want merely because you think it would be good for him. -- Robert Heinlein
| |
| Dan Mercer 2004-07-28, 6:19 pm |
|
"Pascal Bourguignon" <spam@thalassa.informatimago.com> wrote in message news:87658794cm.fsf@thalassa.informatimago.com...
:
: "Dan Mercer" <dmercer@mn.rr.com> writes:
: > "Bigdakine" <bigdakine@aol.comGetaGrip> wrote in message news:20040728052202.15071.00000585@mb-m02.aol.com...
: > : Is there a way in C to add records to the beginning of file, as opposed to the
: > : end?
: > :
: > : Sure there is the sledge hammer approach,
: > :
: > : where I could write the new record to a file and
: > :
: > : system( "cat new_record file > temp; mv temp file")
: >
: > You could also:
: >
: > system("exec 3<file;rm -f file;cat new_file - <&3 >file");
:
: With interesting results when the cat process crashes in the middle of
: copying...
And why would cat crash?
Dan Mercer
:
:
: > Or you could write your own routine, but it's very unlikely to be much
: > more efficient than cat.
:
: --
: __Pascal Bourguignon__ http://www.informatimago.com/
:
: There is no worse tyranny than to force a man to pay for what he does not
: want merely because you think it would be good for him. -- Robert Heinlein
| |
| Måns Rullgård 2004-07-28, 6:19 pm |
| "Dan Mercer" <dmercer@mn.rr.com> writes:
> : > : system( "cat new_record file > temp; mv temp file")
> : >
> : > You could also:
> : >
> : > system("exec 3<file;rm -f file;cat new_file - <&3 >file");
> :
> : With interesting results when the cat process crashes in the middle of
> : copying...
>
> And why would cat crash?
It could run out of disk space. Remember, the file still uses disk
space, even though it has no name. It could also crash due to a
random bit flip in some memory. Such things do happen.
--
Måns Rullgård
mru@kth.se
| |
| Bigdakine 2004-07-28, 6:19 pm |
| >Subject: Re: Writing to beginning of file?
>From: Pascal Bourguignon spam@thalassa.informatimago.com
>Date: 7/28/04 7:36 AM Hawaiian Standard Time
>Message-id: <87isc8825z.fsf@thalassa.informatimago.com>
>
>Rich Teer <rich.teer@rite-group.com> writes:
>
>
>Because this is not a problem of C, shell or language in general, but
>a problem of file system. There is no notion of negative file
>position, so you cannot write before file position 0.
>
>
>If you had been more far-sighted, you could have started to write in
>your file for example at position start=0x1000000, and now you could
>just seek at some position start-size to write size byte.
>
>Of course, all the programs that use this file would have to know
>where the current start of file is. A convenient way to do it, is to
>add a header at the physical start of the file indicating the current
>virtual start of file.
>
>Once you've done that, and given you're telling us that you've got
>"records",
That was loose fingers ony part. The entries in the file do not all have the
same length. Although I can consider making them all the same length via
padding.
<rest of info>
Thanks for the responses.
Stuart
Dr. Stuart A. Weinstein
Ewa Beach Institute of Tectonics
"To err is human, but to really foul things up requires a creationist"
"Creationists aren't impervious to Logic: They're oblivious to it."
| |
| Pascal Bourguignon 2004-07-28, 8:47 pm |
| "Dan Mercer" <dmercer@mn.rr.com> writes:
> : > system("exec 3<file;rm -f file;cat new_file - <&3 >file");
> :
> : With interesting results when the cat process crashes in the middle of
> : copying...
>
> And why would cat crash?
Because the dog tripped on the CPU power cable.
--
__Pascal Bourguignon__ http://www.informatimago.com/
There is no worse tyranny than to force a man to pay for what he does not
want merely because you think it would be good for him. -- Robert Heinlein
| |
| Pascal Bourguignon 2004-07-28, 8:47 pm |
| bigdakine@aol.comGetaGrip (Bigdakine) writes:
>
>
> That was loose fingers ony part. The entries in the file do not all have the
> same length. Although I can consider making them all the same length via
> padding.
But read on the cited manual page, records are not always of fixed size!
--
__Pascal Bourguignon__ http://www.informatimago.com/
There is no worse tyranny than to force a man to pay for what he does not
want merely because you think it would be good for him. -- Robert Heinlein
| |
| Bigdakine 2004-07-28, 8:47 pm |
| >Subject: Re: Writing to beginning of file?
>From: Pascal Bourguignon spam@thalassa.informatimago.com
>Date: 7/28/04 4:28 PM Hawaiian Standard Time
>Message-id: <87u0vr7djf.fsf@thalassa.informatimago.com>
>
>"Dan Mercer" <dmercer@mn.rr.com> writes:
>
>Because the dog tripped on the CPU power cable.
Or it slipped chasing the mouse.
OK. Enuf of that.
Stuart
Dr. Stuart A. Weinstein
Ewa Beach Institute of Tectonics
"To err is human, but to really foul things up requires a creationist"
"Creationists aren't impervious to Logic: They're oblivious to it."
|
|
|
|
|