Reading from file

#TC1017 #WSQ12

This was quite a cool and rather long problem. I feel quite happy about it, only case sensitivity is still an issue. But other than that it is quite comprehensive, I mean, it is user friendly and allows him to correct any input mistakes by checking whether the desired file was found or not.

To accomplish this many special functions were needed, such as .open, ifstream, .is_open, and.eof. But when I understood how these worked, the rest of the code was very intuitive to finsih.

wsq12.PNG

Source Code below: [GitHub Link: https://github.com/diegodamy/WSQ12 %5D

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int CheckFile(string textname3){
ifstream InList;
InList.open(textname3.c_str());

if (InList.is_open()) {
cout << “File was opened succesfully!” << endl;
return 1;
} else {
return 0;
}
}

int ReadFile(string word2, string textname2){
int counter;
string textword;
ifstream InList;
InList.open(textname2.c_str());

while (!InList.eof()) { // keep counting until you rach the last string
InList >> textword; // create a string out of all words in the text file
if (textword == word2) {
counter++;
}
}

InList.close();
return counter;
}
int KeepRunning(char decision){
if (decision == ‘Y’ || decision == ‘y’){
return 1;
} else {
return 0;
}
}

int main(){
string word;
string textname;
char decision;

do {
cout <<“Please enter the name of the text:” << endl;
cin >> textname;

if (CheckFile(textname)==0) {
cout << “Unable to open file.” << endl;
cout <<“Please enter the name of the text:” << endl;
cin >> textname;
}

cout <<“Please enter the word you wish to find in the text:” << endl;
cin >> word;

cout << endl << word << ” was found ” << ReadFile(word, textname) << ” time(s) in the text.” << endl;
cout <<

<< “Do you wish to look for another word?: Y/N” << endl;
cin >> decision;

} while (KeepRunning(decision) == 1);
cout << “Goodbye!” << endl;
}

————————————————-

CC BY-SA 4.0 Reading from file by diegodamy is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.