-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext reader and search.cpp
69 lines (61 loc) · 1.04 KB
/
text reader and search.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
using namespace std;
int main()
{
vector <string> word;
string a;
string szFilename="";
cout<<"Enter name of file to read.\n def for default"<<endl;
cin>>szFilename;
ifstream read;
if(szFilename=="def")
{
read.open("text reader trial.txt");
}else
{
szFilename+=".txt";
read.open(szFilename.c_str());
}
try
{
if(!read.is_open())
{
throw 2020;
}
}
catch(int x)
{
cerr<<"Could not find file!"<<endl;
cout<<"Error "<<x<<endl;
}
while(read>>a)
{
word.push_back(a);
}
static int cnt=word.size();
cout<<"Word count = "<<cnt<<endl;
for(int i=0;i<cnt;i++)
{
cout<<word[i]<<" ";
}
cout<<"\n\n\nEnter word to search: "<<endl;
cin>>a;
string b=(a+",");
static int no=0;
for(int i=0;i<cnt;i++)
{
if(word[i]==a)
{
no++;
}
if(word[i]==b)
{
no++;
}
}
cout<<"\nThe word "<<a<<" has been found "<<no<<" times."<<endl;
return 0;
}