forked from portfoliocourses/cplusplus-example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount_file_lines.cpp
81 lines (65 loc) · 2.65 KB
/
count_file_lines.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*******************************************************************************
*
* Program: Count the total number of lines in a file
*
* Description: Program to count the total number of lines in a file using C++.
*
* YouTube Lesson: https://www.youtube.com/watch?v=xi9TIHWBjPs
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <iostream>
#include <fstream> // included so we can use the ifstream object
using namespace std;
int main(void)
{
// stores the name of the file to be opened
string filename;
// ifstream file object is used to access the file
ifstream file;
// Prompt the user to enter the filename of the file to open and store what
// the user enters into the filename string variable.
cout << "Filename: ";
cin >> filename;
// Use the open member function of the ifstream file object to open the file
// with the filename provided by the user.
file.open(filename);
// If the file fails to open for some reason (e.g. the file doesn't exist),
// the fail member function of the ifstream file object will return true.
// If it does, we exit the program with an error message and status.
if (file.fail())
{
// Output an error message for the user
cout << "File failed to open." << endl;
// Exit the program with an error status. When we use a return statement in
// in the main function the program will terminate, and returning '1'
// (instead of returning 0) is a signal to the shell/terminal/console that
// the program exited with an error status.
return 1;
}
// line string variable to help read each line of the file as a string
string line;
// lines int variable to keep track of how many lines are in the file total
int lines = 0;
// The .eof() member function will return true when we reach the end of the
// file, so we continue the loop so long as this is NOT true using !file.eof()
while (!file.eof())
{
// getline() will read the next line of the file with each loop iteration
// and store it into the string line
getline(file, line);
// output the line
cout << line << endl;
// Increment number of lines to keep track of the total number of lines in
// the file, as we have read another line from the file. Becaus this
// will execute once for each line in the file, once this loop is done
// executing lines will contain the total number of lines in the file.
lines++;
}
// Close the file as we are now done working with it
file.close();
// Output the total number of lines in the file
cout << "Total Lines: " << lines << endl;
return 0;
}