Dear Amadeus,
Under your help, I have constructed the program successfully. Thanks again for your and other kiind man's great help. The following code has already been tested. It can work normally.
I paste the code here for other guys who will meet the same question.
CODE
// String search and insert
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
string filename=argv[1];
filename=filename+".cpp";
//
//Read the file
ifstream CPPfile (filename.c_str());
if (CPPfile.is_open())
{
int size,begin,end;
begin = CPPfile.tellg();
CPPfile.seekg (0, ios::end);
end = CPPfile.tellg();
size = end-begin;
//cout<<"size: "<<size<<endl;
char *memblock;
string filecontent;
memblock=new char[size];
CPPfile.seekg (0, ios::beg);
CPPfile.read (memblock, size);
CPPfile.close();
filecontent=memblock;
delete [] memblock;
//cout<<memblock<<endl;
string::size_type pos = filecontent.find("return 0;",0);
if (pos == string::npos) cout << "Not found" << endl;
string insertCode="cout<<endl;\ncout<<\"Clock ticks: \"<<clock()<<\" Seconds: \"<<clock()/CLOCKS_PER_SEC << endl;\n";
filecontent.insert (pos,insertCode);
// Create a CPP file
ofstream myfile;
myfile.open (filename.c_str());
myfile <<filecontent;
myfile.close();
cout << "Timing code has been inserted successfully!\n\n";
}
else cout << "Unable to open CPP file.";
return 0;
}
This post has been edited by Dark_Nexus: 29 Mar, 2006 - 10:47 PM