Re: fopen: pre-pending text
- From: floyd@xxxxxxxxxx (Floyd L. Davidson)
- Date: Sat, 28 Jan 2006 04:45:50 -0900
joe.user0@xxxxxxxxx wrote:
>Coding in C, I can append text to a file
>e.g. as with
>
> fopen ("txtfile", a+)
>
>To write at the begin of file, I can use
>
> fopen ("txtfile", r+)
>
>but that will overwrite existing text.
>
>So how do I insert (pre-prend) text with fopen?
1) open original file for reading
2) open a temp file for writing
3) write new text to temp file
4) write original file to end of temp file
5) close both files
6) verify temp file (using stat or whatever) has desired data
7) open temp file for reading
8) open original file for writing at start of file
9) write temp file to original file
10) close both files
11) verify original file now has new data
12) unlink temp file
Of course if the data is not particularly sensitive, and losing
it would not be the end of the world, several steps there can be
eliminated.
--
Floyd L. Davidson <
http://www.apaflo.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) floyd@xxxxxxxxxx
.
Relevant Pages
- Re: Bash help requested: Capturing command errors within pipes
... The reason for this was because we sometimes needed to access variables by name at link-time and the CLI (Command Line Interface, a sort of macro processor) was case insensitive, so all commands caused everything to be flipped to uppercase, no matter what we wanted. ... and uniq'd original file to a temp file, ... => *)echo exit ok and PIPESTATUS all zeroes ... (Fedora) - Re: Modifying a file w/o creating a temp file
... ease of use, if the original file had hard links to it, they are ... space equal to the size of the temp file (unless the temp file is ... written to a different file system which raises its own issues). ... lost the file entirely. ... (comp.lang.java.programmer) - Re: Modifying a file w/o creating a temp file
... Of course there's one simple and obvious solution which has not yet been mentioned or rejected: read the whole file into memory, modify it there, truncate the original file, and write the modified data into it. ... This has substantial subtle benefits, balanced against the one flaw of being memory hungry and limited by swap space. ... Benefits include ease of use, if the original file had hard links to it, they are not broken, if it had special modes/attributes/ACLs, they are not lost, and it never needs more space than the file system has, whereas a temp-file solution typically needs an amount of unused disk space equal to the size of the temp file. ... (comp.lang.java.programmer) - Re: fopen: pre-pending text
... >> 2) open a temp file for writing ... >> 4) write original file to end of temp file ... >right after verifiying. ... >> 8) open original file for writing at start of file ... (comp.os.linux.development.system) - Re: File manipulation
... >> I'm getting confused on the login. ... one easy way to do that is to use a temp file: ... > * open original file ... > * move the temp file over the original file ... (perl.beginners) |
|