On Aug 5, 6:09*am, InuY4sha <inuy4...@gmail.com> wrote:
> Hi folks..
> say I'd wish to replace a token in a file... what's the plainest
> (stdio allowed) fastest way? Do I have to copy the whole file? Isn't
> there a cleaner way?
From the C-FAQ:
12.30: I'm trying to update a file in place, by using fopen mode "r+",
reading a certain string, and writing back a modified string,
but it's not working.
A: Be sure to call fseek before you write, both to seek back to
the
beginning of the string you're trying to overwrite, and because
an fseek or fflush is always required between reading and
writing in the read/write "+" modes. Also, remember that you
can only overwrite characters with the same number of
replacement characters, and that overwriting in text mode may
truncate the file at that point, and that you may have to
preserve line lengths. See also question 19.14.
References: ISO Sec. 7.9.5.3.
19.14: How can I insert or delete a line (or record) in the middle of
a
file?
A: Short of rewriting the file, you probably can't. The usual
solution is simply to rewrite the file. (Instead of deleting
records, you might consider simply marking them as deleted, to
avoid rewriting.) Another possibility, of course, is to use a
database instead of a flat file. See also questions 12.30 and
19.13.
Those who read the C-FAQ have 33% fewer cavities than those who don't.
|