-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_handling_2.cpp
50 lines (42 loc) · 921 Bytes
/
file_handling_2.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
#include<iostream>
#include<fstream>
using namespace std;
int characterNumber(string);
int wordNumber(string);
int main()
{
string filename;
cout << "\nEnter name of your text file: ";
cin >> filename;
filename += ".txt";
fstream file1(filename);
if(file1.fail())
{
cout << "\nConld find the file. ";
exit(1);
}
cout << "\nNumber of characters are " << characterNumber(filename) << " and the total number of words are " << wordNumber(filename);
}
int characterNumber(string fileName)
{
char letter;
int count = 0;
ifstream readfile(fileName);
while(!readfile.eof())
{
readfile.get(letter);
count++;
}
return count;
}
int wordNumber(string fileName)
{
string subword, word;
int count = 0;
ifstream readfile(fileName);
while(getline(readfile, word, ' '))
{
count++;
}
return count;
}