C++ program to find that a given word is present in the file or not.
#include<fstream>#include<iostream>#include<string.h>usingnamespacestd;
intmain(){
ifstream fin; //object is created of ifstream
fin.open("Word1.txt",ios::in);//ios::in opens the file for reading i.e.,in input mode char word[100],w[50];
int c=0;
cout<<"Enter the word which u have to search:";
gets(w);
while(!fin.eof())
{
fin>>word; //Getting the word from the fileif(strcmp(word,w)==0) //Comparing each word from the file with the given word
c++; //Counting the given word
}
if (c!=0) //checking if the word is present or notcout<<"Given word is present in the file";
elsecout<<"Given word is not present in the file";
fin.close();// it flushes the buffer before terminating the connection of the file
}
Comments
Hatim
18-Jan-2019 09:17:02 PM