 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Alex Guest
|
Posted: Fri Jul 16, 2004 11:12 am Post subject: How Can I replace some info in a file use iostream ? |
|
|
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
|
Posted: Tue Jul 20, 2004 9:21 pm Post subject: Re: How Can I replace some info in a file use iostream ? |
|
|
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 |
|
 |
|
|
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
|
|