Re: Writing a character to the beginning of the same file



If you have a *nix system, look up the cat command and use it at the command
line. On Windows, you can use

$> copy file1+file2 /B file3

On both systems, simply add the characters you wish to be prepended into
file1.

If you *must* do it programmatically, you can try to place them in a call
using system(char *) in stdlib.h.

system("cat file1 file2"); // I think
system("copy file1+file2 /B file3");

Alternatively, you can create a large buffer and use the functions fread,
fwrite, etc. to read in a large part of the file at once.

For an example, look at www.stickypalms.com/cpp/prepend.c.txt
I think it should compile on any system - I could be wrong. It's been a
while since I did strict c coding :)



"SAM" <mshyamrao@xxxxxxxxx> wrote in message
news:1114685495.313105.177700@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
>
> Randy Howard wrote:
> > In article <1114660481.571702.267690@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
> > mshyamrao@xxxxxxxxx says...
> > > So what is the solution to write a character to the beginning of
> the
> > > file and not affect the existing content.
> >
> > Homework notwithstanding consider the following:
> >
> >
> > If it sufficiently small to store in memory
> > open the file
> > read the whole file into dynamic storage
> > close the file
> > else
> > copy the file (or rename it) somewhere else
> >
> > open the file for writing positioned at the beginning of the file
> > write the single new character
> >
> > Depending on the conditional above
> >
> > either write the contents straight from dynamic storage into the
> > file just like the last character
> > or
> > read from the copy/renamed version of the original file and
> > write into the new one
> >
> > close the file
> > exit
> >
> > If that doesn't get you anywhere, drop the class or hire a tutor.
> >
> > --
> > Randy Howard (2reply remove FOOBAR)
> > "Making it hard to do stupid things often makes it hard
> > to do smart ones too." -- Andrew Koenig
>
> Randy thanks very much. I thought someone would give me a better
> solution than this. I had worked on ur solution but it had taken lots
> of time to complete the process in the case of large files.
>
> Any way thankx very much.
>
> SAM
>


.