Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Replacing in file

Reply
Thread Tools

Replacing in file

 
 
InuY4sha
Guest
Posts: n/a
 
      08-05-2009
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?

Thanks!
RM
 
Reply With Quote
 
 
 
 
Alexander Bartolich
Guest
Posts: n/a
 
      08-05-2009
InuY4sha wrote:
> [...]
> 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?


If the size of the replacement does not equal the size of the original
token then the answer is no.

--
Brüder, in die Tonne die Freiheit,
Brüder, ein Stoppschild davor.
Egal was die Schwarzen Verlangen
Rufen wir: Ja! Brav im Chor.
 
Reply With Quote
 
 
 
 
InuY4sha
Guest
Posts: n/a
 
      08-05-2009

> If the size of the replacement does not equal the size of the original
> token then the answer is no.
>


Interesting.. otherwise?

 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      08-05-2009
InuY4sha <> writes:

>> If the size of the replacement does not equal the size of the original
>> token then the answer is no.
>>

>
> Interesting.. otherwise?


The only portable way involves copying to some other file, replacing
the token on the way. Afterwards you delete the old file and rename
the new one.

If the tokens are the same size or the new on is larger, you can open
the file for reading and writing and scan for the token. Remember
where it is and then store everything that follows in memory. Return
to your saved position and write the new token and your saved data.

On many systems this works even when the new token is smaller, but
that relies on non-stdio guaranteed file truncation of text files.

--
Ben.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      08-05-2009
Richard Heathfield <> writes:

> InuY4sha said:
>
>> 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?

>
> If the token you are inserting is the same size as the token you are
> replacing, you can simply find and overwrite the existing token. Do
> your search, find the point in the file where the old token starts,
> and write your new token there. Then you can close the file if you
> like.


If the files are opened as text files, might this not truncate the
file? 7.19.3 p2 certainly suggest that it might. Of course you may
be assuming binary streams, but it seems unwise to assume that.

<snip answer that is otherwise much fuller and more helpful than mine>
--
Ben.
 
Reply With Quote
 
InuY4sha
Guest
Posts: n/a
 
      08-05-2009

> Richard Heathfield <http://www.cpax.org.uk>
> Email: -http://www. +rjh@
> "Usenet is a strange place" - dmr 29 July 1999
> This line unintentionally left unblank


Thank you, I learnt a lot today.
 
Reply With Quote
 
user923005
Guest
Posts: n/a
 
      08-05-2009
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[File Input Module]Replacing string in a file vanam Python 1 01-29-2010 06:01 AM
Replacing - and not Replacing... Rob Meade ASP General 5 04-11-2005 06:49 PM
replacing chars in a text file dornick C++ 3 03-02-2005 03:28 AM
"Virtual" file system mock object - replacing stuff in __builtins__ Remy Blank Python 3 03-06-2004 12:20 AM
Replacing line in a file Chad C++ 6 11-17-2003 05:26 PM



Advertisments