File tree Expand file tree Collapse file tree 3 files changed +55
-1
lines changed
14_file_handling/02_read_file_and_cout_chars Expand file tree Collapse file tree 3 files changed +55
-1
lines changed Original file line number Diff line number Diff line change 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\n Number of Characters in File => " << count;
48+
49+ // // close file
50+ fcin.close ();
51+
52+ cout << endl; // Add new line
53+ return 0 ;
54+ }
Original file line number Diff line number Diff line change 1+ Web With Aman
You can’t perform that action at this time.
0 commit comments