Try this.
Code:
#include <fstream.h>
int Search(const char [], const char [], ifstream &, char []);
int main(void)
{
const int BUFF_LEN=100;
char FileName[BUFF_LEN], Pattern[BUFF_LEN], Buffer[BUFF_LEN];
int PatternCount;
ifstream File;
cout << "Please enter file name: ";
cin.getline(FileName, sizeof File);
File.open(FileName);
if(!File)
cout << "Could not open file....";
else
{
cout << "Please enter the patten you want to look for: ";
cin.getline(Pattern, sizeof Pattern);
PatternCount = Search(FileName, Pattern, File, Buffer);
cout << "The file " << FileName << " has the word(s) " << Pattern << " "
<< PatternCount << " times.";
}
cin.get();
return 0;
}
int Search(const char FileName[], const char Pattern[], ifstream &File,
char Buffer[])
{
int Occurances=0;
while(!File.eof())
{
File >> Buffer;
if(strcmp(Buffer, Pattern)==0)
Occurances++;
}
return Occurances;
}