Re: Editing Files: Help!



ttrium wrote:
Ok, so this is probably the easist question ever posted, but I
honestly can't find the answer (if there is one).

How do I (by which function) add data to a file at the beginning of a
file, rather than the end of the file (as is with fopen(file, "a"))?


Two ways:

1. Open the file, read it all into memory, truncate the file, write the new data followed by the old data.

2. Open the file, open a temporary file, write the new data to the temporary file, copy the data from the original file to the temporary file, close both files. delete the original file and rename the temporary file.

#2 is obviously more work, but it doesn't require lots of memory to read a large file into memory, and won't lose the file if the system crashes in the middle.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@xxxxxxxxxxxxx
==================
.



Relevant Pages

  • Re: What is an "anonymous temporary file?" (question about PerlFAQ5)
    ... > it is a "file" in memory only. ... It is an actual file on an actual filesystem, ... > of an actual disk-based temporary file in the usual places (/tmp, ... linux automatically cleans them up. ...
    (comp.lang.perl.misc)
  • Re: Booting Pavilian From CD.
    ... TrueCrypt claims that all decryption is done in memory. ... can be paged out and will thus appear in the paging file. ... It's certainly not rare to find snippets of sourcecode and other data ... When you save a file from many programs, they save a temporary file ...
    (comp.sys.laptops)
  • Re: df reports a drive as a 100% full but cannot find the files with du
    ... without deleting the temporary file, ... temporary stuff you want in the memory as well, ...
    (comp.os.linux.misc)
  • Re: df reports a drive as a 100% full but cannot find the files with du
    ... without deleting the temporary file, ... The kernel will page stuff to HDD anyways if it runs out of memory so space ought not to be a factor I guess. ... Unlike MULTICS which had 2^18 segments of 2^18 words of virtual memory, UNIX systems, especially on something like a DEC PDP-11 had much more modest capacities. ...
    (comp.os.linux.misc)
  • RE: Locked for editing???
    ... removable disk, such as a floppy diskette, the disk has been removed. ... file name and places this file in the same folder as the original file. ... The process was interrupted while creating the temporary file because you ... If the interruption was caused ...
    (microsoft.public.excel.setup)