C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How Can I replace some info in a file use iostream ?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Alex
Guest





PostPosted: Fri Jul 16, 2004 11:12 am    Post subject: How Can I replace some info in a file use iostream ? Reply with quote



How can I replace all "12345" to "789" in a very large file use C++'s
iostream?

istream_iterator and ostream_iterator seems can't help



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ben Hutchings
Guest





PostPosted: Tue Jul 20, 2004 9:21 pm    Post subject: Re: How Can I replace some info in a file use iostream ? Reply with quote



Alex wrote:
Quote:
How can I replace all "12345" to "789" in a very large file use C++'s
iostream?
snip


You can't perform this change in-place because there is no portable
way to truncate a file and generally no way to remove bytes from the
middle of it either. (If the replacement string was the same length,
this might be doable, but if you open the file in text mode I don't
think even that is certain.)

So you need to create a new file. You can read through the original
file a line at a time with std::getline(), performing the replacement
on each line and writing it out. Finally you can remove() the original
file and rename() the new file into its place.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.