 |
|
 |
|
|
 |
Writing to beginning of file? |
 |
 |
|
|
07-28-04 11:19 PM
Is there a way in C to add records to the beginning of file, as opposed to t
he
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
[ Post a follow-up to this message ]
|
|
|
 |
|
|
|
 |
Re: Writing to beginning of file? |
 |
 |
|
|
 |
|
 |
|
|
 |
Re: Writing to beginning of file? |
 |
 |
|
|
07-28-04 11: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 numbe
r 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
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Writing to beginning of file? |
 |
 |
|
|
07-28-04 11: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 inserti
on!
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
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Writing to beginning of file? |
 |
 |
|
|
07-28-04 11:19 PM
"Dan Mercer" <dmercer@mn.rr.com> writes:
> "Bigdakine" <bigdakine@aol.comGetaGrip> wrote in message news:200407280522
02.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
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Writing to beginning of file? |
 |
 |
|
|
07-28-04 11: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:2004072805
2202.15071.00000585@mb-m02.aol.com...
: > : Is there a way in C to add records to the beginning of file, as oppose
d 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
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Writing to beginning of file? |
 |
 |
|
|
07-28-04 11: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
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Writing to beginning of file? |
 |
 |
|
|
07-28-04 11: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."
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Writing to beginning of file? |
 |
 |
|
|
07-29-04 01:47 AM
"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
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: Writing to beginning of file? |
 |
 |
|
|
07-29-04 01:47 AM
bigdakine@aol.comGetaGrip (Bigdakine) writes:
>
>
> That was loose fingers ony part. The entries in the file do not all have t
he
> 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
[ Post a follow-up to this message ]
|
|
|
 |
|
|
|
|
Sponsored Links |
 |
 |
|
|
 |
All times are GMT. The time now is 11:18 AM. |
 |
|
|
 |
|
 |
|
|
 |
|
Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
|
|
|
|
Medical and Health forum | Computer Games Reviews | Graphics design forum
|
 |
|
 |
|