Skip to content

Commit 8c502dd

Browse files
committed
add: 14_file_handling 02
1 parent dc9d170 commit 8c502dd

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed
Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,54 @@
1-
// // Write a C++ program to read a text file and count the number of characters in it.
1+
// // Write a C++ program to read a text file and count the number of characters in it.
2+
3+
// // Header files
4+
#include <iostream>
5+
#include <fstream>
6+
7+
// // use namespace
8+
using namespace std;
9+
10+
int main()
11+
{
12+
13+
// // specify file name
14+
const char *fileName = "read.txt";
15+
16+
// // create an instance of ofstream
17+
ifstream fcin;
18+
19+
// // open file for reading
20+
fcin.open(fileName, ios::in);
21+
22+
// // check if the file is successfully opened
23+
if (!fcin.is_open())
24+
{
25+
cout << "\n!!! Unable to Open File...\n";
26+
return 1;
27+
}
28+
29+
// // read character by character from file and count
30+
char ch;
31+
int count = 0;
32+
33+
cout << "\n>>>>>>>>> Following is the Content Read From File <<<<<<<<<<\n";
34+
35+
// // get one character from file
36+
ch = fcin.get();
37+
38+
while (!fcin.eof())
39+
{
40+
cout << ch;
41+
count++;
42+
43+
// // get one character from file
44+
ch = fcin.get();
45+
}
46+
47+
cout << "\n\nNumber of Characters in File => " << count;
48+
49+
// // close file
50+
fcin.close();
51+
52+
cout << endl; // Add new line
53+
return 0;
54+
}
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Web With Aman

0 commit comments

Comments
 (0)