www.digitalmars.com         C & C++   DMDScript  

D - file manipulation: is this possible?

reply "Andrew Edwards" <crxace13 comcast.net> writes:
Is there a way to add information to the beginning of a file without
overwriting the content?

if at all possible, please provide the d way and a c++ equivalent.

only way I now to do it is to use two files, adding the new info to a temp
file, copying old file to end of temp file and then deleting old file and
renaming temp.  However, I'd like to manipulate the file internally.
Oct 06 2002
parent reply "Walter" <walter digitalmars.com> writes:
"Andrew Edwards" <crxace13 comcast.net> wrote in message
news:anqpl2$2gdi$1 digitaldaemon.com...
 Is there a way to add information to the beginning of a file without
 overwriting the content?
No.
 if at all possible, please provide the d way and a c++ equivalent.

 only way I now to do it is to use two files, adding the new info to a temp
 file, copying old file to end of temp file and then deleting old file and
 renaming temp.  However, I'd like to manipulate the file internally.
I'd use an in-memory buffer, so no need to write multiple files.
Oct 06 2002
next sibling parent reply "Andrew Edwards" <crxace13 comcast.net> writes:
"Walter" <walter digitalmars.com> wrote in message
news:anr2th$2ps1$1 digitaldaemon.com...
|
| "Andrew Edwards" <crxace13 comcast.net> wrote in message
| news:anqpl2$2gdi$1 digitaldaemon.com...
| > Is there a way to add information to the beginning of a file without
| > overwriting the content?
|
| No.

Don't you find it necessary to do things like this?  How do you accomplish
the task during those times?  Is it simply not used enough to warrant
adressing this issue?

| > if at all possible, please provide the d way and a c++ equivalent.
| >
| > only way I now to do it is to use two files, adding the new info to a
temp
| > file, copying old file to end of temp file and then deleting old file
and
| > renaming temp.  However, I'd like to manipulate the file internally.
|
| I'd use an in-memory buffer, so no need to write multiple files.
|
example please?
Oct 07 2002
parent "Walter" <walter digitalmars.com> writes:
"Andrew Edwards" <crxace13 comcast.net> wrote in message
news:anrkq8$bv2$1 digitaldaemon.com...
 "Walter" <walter digitalmars.com> wrote in message
 news:anr2th$2ps1$1 digitaldaemon.com...
 | "Andrew Edwards" <crxace13 comcast.net> wrote in message
 | news:anqpl2$2gdi$1 digitaldaemon.com...
 | > Is there a way to add information to the beginning of a file without
 | > overwriting the content?
 | No.
 Don't you find it necessary to do things like this?  How do you accomplish
 the task during those times?  Is it simply not used enough to warrant
 adressing this issue?
It comes down to the way the operating system writes files to the disk. If you want to prepend to a file, the file will need to be rewritten. It's not a big problem, just a routine programming chore.
 | > if at all possible, please provide the d way and a c++ equivalent.
 | >
 | > only way I now to do it is to use two files, adding the new info to a
 temp
 | > file, copying old file to end of temp file and then deleting old file
 and
 | > renaming temp.  However, I'd like to manipulate the file internally.
 |
 | I'd use an in-memory buffer, so no need to write multiple files.
 |
 example please?
Here's some pseudo code to illustrate the algorithm: byte[] a; // data to prepend byte[] b; byte[] c; <read file into b[]> c = a ~ b; // create buffer containing new file data <write c[] into file>
Oct 07 2002
prev sibling parent reply "Sandor Hojtsy" <hojtsy index.hu> writes:
"Walter" <walter digitalmars.com> wrote in message
news:anr2th$2ps1$1 digitaldaemon.com...
 "Andrew Edwards" <crxace13 comcast.net> wrote in message
 news:anqpl2$2gdi$1 digitaldaemon.com...
 Is there a way to add information to the beginning of a file without
 overwriting the content?
No.
The limitation is not in the languages, but in the Operating System, and the File System.
 if at all possible, please provide the d way and a c++ equivalent.

 only way I now to do it is to use two files, adding the new info to a
temp
 file, copying old file to end of temp file and then deleting old file
and
 renaming temp.  However, I'd like to manipulate the file internally.
I'd use an in-memory buffer, so no need to write multiple files.
Maybe a library could do this for you, emulating the possibility of insertion. But it would be so extremely slow that noone intended to implement it.
Oct 07 2002
parent reply "Robert W. Cunningham" <FlyPG users.sourceforge.net> writes:
Back in the early '80s I helped hack the BSD 4.0 filesystem seek()/fseek()
calls to accept negative offsets from the beginning of the file.  We had to
kluge the inodes, but we made it work.  The hack would be automagically undone
the next time the file was rewritten.  The main change was allowing the first
block in a file to start with unallocated/free space, so the first byte of the
file could be at any offset within the first block.

It was extremely easy to completely screw up a file using this capability, but
it still had lots of nifty uses.

But you have to reach deep inside the filesystem to make it happen.


-BobC



Sandor Hojtsy wrote:

 "Walter" <walter digitalmars.com> wrote in message
 news:anr2th$2ps1$1 digitaldaemon.com...
 "Andrew Edwards" <crxace13 comcast.net> wrote in message
 news:anqpl2$2gdi$1 digitaldaemon.com...
 Is there a way to add information to the beginning of a file without
 overwriting the content?
No.
The limitation is not in the languages, but in the Operating System, and the File System.
 if at all possible, please provide the d way and a c++ equivalent.

 only way I now to do it is to use two files, adding the new info to a
temp
 file, copying old file to end of temp file and then deleting old file
and
 renaming temp.  However, I'd like to manipulate the file internally.
I'd use an in-memory buffer, so no need to write multiple files.
Maybe a library could do this for you, emulating the possibility of insertion. But it would be so extremely slow that noone intended to implement it.
Oct 08 2002
parent "Roberto Mariottini" <rmariottini lycosmail.com> writes:
"Robert W. Cunningham" <FlyPG users.sourceforge.net> ha scritto nel
messaggio news:3DA3873E.C95307F4 users.sourceforge.net...
 Back in the early '80s I helped hack the BSD 4.0 filesystem seek()/fseek()
 calls to accept negative offsets from the beginning of the file.  We had
to
 kluge the inodes, but we made it work.  The hack would be automagically
undone
 the next time the file was rewritten.  The main change was allowing the
first
 block in a file to start with unallocated/free space, so the first byte of
the
 file could be at any offset within the first block.

 It was extremely easy to completely screw up a file using this capability,
but
 it still had lots of nifty uses.

 But you have to reach deep inside the filesystem to make it happen.
Yes. I always stated it was possible and EASY to do, but I couldn't prove it. The full story is that operating systems were invented when only magnetic tapes and punched cards were available. This particular kind of devices limited file access to a sequential start-to-end operation. And the only way to add some data to a file was to append to it, because before this file there could be another file in the tape/card sequence. Today, with random access block devices as disks, if it's not yet possible to do it is due only to the operating system designers' lazyness. I had to patch a program that filled the disk every time the user attempted to add a little image file to a big image library: it copied away the whole library only to add a little one. Why? The image index was at the beginning of the library file, so to update it the librarian program had to make a copy of the whole library. My patch was to put the index at the end of the library file, but it could be unnecessary if the OS permitted to enlarge a file from the beginning and not only from the end. Ciao
Oct 09 2002