C++: Globally Replace Text in Several Files

Bjarne-stroustrup
 

The task is to replace every occurring instance of a piece of text in a group of text files with another one. For this task we want to replace the text “Goodbye London!” with “Hello New York!” for a list of files.

#include <fstream>
#include <iterator>
#include <boost/regex.hpp>
#include <string>
#include <iostream>

int main( int argc , char *argv[ ] ) {
	boost::regex to_be_replaced( "Goodbye London\\s*!" ) ;
	std::string replacement( "Hello New York!" ) ;
	for ( int i = 1 ; i < argc ; i++ ) {
		std::ifstream infile ( argv[ i ] ) ;
		if ( infile ) {
			std::string filetext( (std::istreambuf_iterator<char>( infile )) ,
			std::istreambuf_iterator<char>( ) ) ;
			std::string changed ( boost::regex_replace( filetext , to_be_replaced , replacement )) ;
			infile.close( ) ;
			std::ofstream outfile( argv[ i ] , std::ios_base::out | std::ios_base::trunc ) ;
			if ( outfile.is_open( ) ) { 
				outfile << changed ;
				outfile.close( ) ;
			}
		}
		else 
		std::cout << "Can't find file " << argv[ i ] << " !\n" ;
	}
	return 0 ;
}

SOURCE

Content is available under GNU Free Documentation License 1.2.

Subscribe to TFE Times

Enter your email address to become a member of TFE Times today!