1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
#include <string>
#include <fstream>
#include <cstdlib>
#include <iostream>
using namespace std;
void FindWord(char[], char[], double);
int main()
{
ifstream inFile;
ofstream outFile;
string line;
inFile.open("AssignmentOne.txt");
if (inFile.fail())
{
cout<<"\nThe file was not successfully opened"
<<"\n Please check that the file currently exists."
<<endl;
exit(1);
}
cout<<"The file exists; opening.\n";
outFile.open("Output.txt");
//read and distplay the files contents
char a[100];
double count=1;
while(getline(inFile,line))
{
inFile >> a[100];
FindWord("cin",a,count);
FindWord("cout",a,count);
FindWord("main",a,count);
FindWord("return",a,count);
FindWord("include",a,count);
count++;
}
inFile.close();
outFile.close();
system ("Pause");
return 0;
}
void FindWord(char a[], char b[], double count)
{
int i,j,startPlace;
for(int k=0;a[k]!=0;k++); //determine the lenght of the magic word
for(int i=0;b[i]!=0;i++)
{
int k=0;
while((a[k+i]==b[k]) && (k<j) &&(a[k+i]!='\0'))
{
k++;
}
if(k==j)
{
startPlace=i+1;
cout<<"The magic word \""<<a<<"\" is found at column "<<startPlace
<<" of line "<<count<<".\n";
}
}
}
| |