Created
May 1, 2016 21:25
-
-
Save batmantec/3b759fb33269485198373393223fb4cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <string> | |
using namespace std; | |
int CheckFile(string fileName){ | |
ifstream InList; | |
InList.open(fileName.c_str()); | |
if (InList.is_open()) { | |
cout << "File Open" << endl; | |
cout << endl; | |
return 1; | |
} else { | |
return 0; | |
} | |
} | |
int ReadFile(string word, string fname){ | |
int counter; | |
string foundWord; | |
ifstream InList; | |
InList.open(fname.c_str()); | |
while (!InList.eof()) { | |
InList >> foundWord; | |
if (foundWord == word) { | |
counter++; | |
} | |
} | |
InList.close(); | |
return counter; | |
} | |
int main(){ | |
string word, fname; | |
cout <<"Enter the File name with .txt at the end:" << endl; | |
cin >> fname; | |
if (CheckFile(fname)==0) { | |
cout << "Please check the File name and location." << endl; | |
} | |
cout <<"Enter the word to search: " << endl; | |
cin >> word; | |
cout << endl << "The word " << '"' << word << '"' <<" was found " << ReadFile(word, fname) << " time(s) in the File." << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment