-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq69.cpp
52 lines (43 loc) · 1.23 KB
/
q69.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
/* WAP to create a file and count the number of characters and digits in that
* file. */
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int num_check(char c) {
if ((48 <= c) && (c <= 57)) {
return 1;
}
else {
return 0;
}
}
int main() {
ofstream file_write("test.txt", ios::out);
string text = "The quick brown fox jumped over the lazy little dog.\n"
"The earth is 150,000,000 km away from the sun! \n"
"There are 30,000,000,000,000 cells in the human body on average.\n";
file_write << text;
file_write.close();
ifstream file_read("test.txt", ios::in);
if (!file_read) {
cerr << "Error: File cannot be opened for reading.\n";
}
char ch;
int num_count = 0, character_count = 0;
cout << "File contents: \n";
while (file_read.get(ch)) {
if (num_check(ch)) {
num_count++;
}
else {
character_count++;
}
cout << ch;
}
cout << "\nSymbol count:\n";
cout << setw(15) << left << "Digits: " << right <<setw(4) << num_count << '\n';
cout << setw(15) << left << "Characters: " << right << setw(4) <<
character_count << '\n';
return 0;
}