@@ -33,45 +33,33 @@ int main()
3333 int charCounter = 0 , wordCounter = 0 , lineCounter = 0 ;
3434 char word[WORD_SIZE]; // to store word extracted from file
3535
36- // // get one character from file
37- ch = fin.get ();
38-
39- if (fin.eof ())
40- cout << " \n File is Empty..." ;
41- else
36+ // // count characters and lines
37+ while (fin.get (ch)) // run till the end of the file
4238 {
43- cout << " \n >>>>>>>>> Following is the Content Read From File <<<<<<<<<<\n " ;
44- lineCounter++;
45- }
46-
47- while (!fin.eof ()) // run till the end of the file
48- {
49- cout << " \n tell g => " << fin.tellg ();
50-
51- cout << ch; // display read character
5239 charCounter++; // increment charCounter
5340
41+ if (charCounter == 1 )
42+ lineCounter++;
43+
5444 // // if new line character occurs
5545 if (ch == ' \n ' )
5646 lineCounter++;
57-
58- // // get one character from file
59- ch = fin.get ();
6047 }
6148
62- cout << " \n tell g => " << fin.tellg ();
49+ // // clear the end-of-file flag and reset the stream state
50+ fin.clear ();
6351
52+ // // set the position to the beginning
6453 fin.seekg (1 );
6554
66- cout << " \n tell g => " << fin.tellg ();
67-
55+ // // count words
6856 while (fin >> word) // run till the end of the file
6957 wordCounter++;
7058
7159 // // display number of characters,words and lines
72- cout << " \n\ n Number of Characters in File => " << charCounter << endl ;
73- cout << " Number of Words in File => " << wordCounter << endl ;
74- cout << " Number of Lines in File => " << lineCounter << endl ;
60+ cout << " \n Number of Characters in File => " << charCounter;
61+ cout << " \n Number of Words in File => " << wordCounter;
62+ cout << " \n Number of Lines in File => " << lineCounter;
7563
7664 // // close file
7765 fin.close ();
0 commit comments