Skip to content

Commit 8e59b2f

Browse files
committed
add: 14_file_handling 06
1 parent dc3acd3 commit 8e59b2f

File tree

4 files changed

+14
-26
lines changed

4 files changed

+14
-26
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"files.associations": {
33
"fstream": "cpp",
4-
"*.tcc": "cpp"
4+
"*.tcc": "cpp",
5+
"ostream": "cpp"
56
}
67
}

14_file_handling/06_count_chars_words_lines/06_count_chars_words_lines.cpp

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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 << "\nFile 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 << "\ntell 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 << "\ntell 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 << "\ntell 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\nNumber 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 << "\nNumber of Characters in File => " << charCounter;
61+
cout << "\nNumber of Words in File => " << wordCounter;
62+
cout << "\nNumber of Lines in File => " << lineCounter;
7563

7664
// // close file
7765
fin.close();
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
aman kaum

14_file_handling/read.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)