I want to create a program that asks the user to input a word ,
then it searches the word in the given txt file. So far what i have been able to do is ,
i have used the getline function, to display the contents , then i have converted the std::string line
to c-string to get tokens ,now i want to compare these tokens with the word that the user entered,
but i dun know how.
Code:#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <string.h>
using namespace std;
int main ()
{
char * cstr;
char * p;
string line;
fstream myfile;
char delim[] =" :,.-;!?_-()";
char arr[12];
cout<<"Enter a word to check whether it is in the text file:";
cin>>arr;
myfile.open ("Ahoo.txt");
if (myfile.is_open())
{
while (!myfile.eof())
{
getline (myfile,line);
cstr = new char [line.size()+1];
strcpy (cstr, line.c_str());
p=strtok (cstr,delim);
while (p!=NULL)
{
cout << p << endl;
p=strtok(NULL,delim);
}
delete[] cstr;
}
myfile.close();
}
else
{
cout<<"File not opened";
}
return 0;
}