a program to match string in a given document

#include <FSTREAM.H>
#include <IOSTREAM.H>
#include <STRING.H>
#include <STRING.H>
#include <STDLIB.H> //for exit



int main(int argc,char *argv[])
{
if(argc!=3) //argc should be 3 for correct execution
{
cout<<"Please enter 2 valid command-line arguments! \n";
}
else
{
ifstream the_ifile(argv[2]); //argv[2] is the filename to open
ofstream the_ofile("counter.txt", ios::app);
if (!the_ifile.IS_OPEN()) //checking to see if file opening succeeded
{
cout << "Could not open file\n";
}
else
{

int occurances=0;
int i,n;

int n = FILE.tellg(ios::end);

char string[n+1];
do //Does an infinite loop if the input file has a new
{ //line. The file should have a continuous sentence.
the_ifile.get(string,n+1);
if(strstr(string, argv[1])!=NULL)
{
occurances++;
}
the_ifile.seekg(i++);
}while(!the_ifile.eof());

the_ofile << "FILE: " << argv[2] << endl;
the_ofile << "SEARCHKEY: " << argv[1] << endl;
the_ofile << "OCCURANCES: " << occurances << endl;
the_ofile << "---------------------------------------" << endl;
}

the_ifile.close();
the_ofile.close();

return 0;
}
}
plz help me to correct the program.........
Have up tried to debug it yourself? If you don't have a debugger, you can place output statements to show the value of variables throughout the run.
Topic archived. No new replies allowed.