C++ program to find that a given word is present in the file or not.














































C++ program to find that a given word is present in the file or not.



#include<fstream> #include<iostream> #include<string.h> using namespace std; int main() { 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 file if(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 not cout<<"Given word is present in the file"; else cout<<"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
    Don't use gets() function its very dangerous it does not check the buffer,instead use string and cin they are more safe and efficient.