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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//---------------------------
//Line Counter
//---------------------------
//Created by Mike Marek
//count.cpp
//Drag n' drop files files over the line counter program and it will display the number of lines in that file
//----------------------------------------------------------------------------------------------------------------------------------------------
 
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <iostream>
#include <conio.h>
 
using namespace std;
//----------------------------------------------------------------------------------------------------------------------------------------------
 
//number of lines in a file
//isolated file name from loaded file
//file program output
vector <int>        lines;
vector <string> fileName;
vector <string> output;
 
//temporary buffer for storing file names
string tmp_filename = "";
 
//layout and properties of the final output display
//---
 
//number of character spaces allowed in the first column
int column1Size = 30;
//caption for the first column (file name)
string column1Caption = "file name";
 
//number of character spaces allowed in the first column
int column2Size = 40;
//caption for the first column (file name)
string column2Caption = "number of lines";
 
//---
//----------------------------------------------------------------------------------------------------------------------------------------------
 
//convert an integer into a string
string intToStr(int number)
{
       // buffer to store the new string
       stringstream buffer;
       buffer << number;
 
       //return the new string
       return buffer.str();
}
//----------------------------------------------------------------------------------------------------------------------------------------------
 
//main function - entry point for our program
int main(int argc, char *argv[])
{
       //an array of input stream files (stores all of our files the user wants read)
       ifstream file[argc - 1];
 
       //loop through all of the files loaded and gather the number of lines from each file
       for (unsigned int i = 1; i < argc; i++)
       {
              //open the file and gather the total amount of lines in that file
              file[i - 1].open(argv[i]);
 
              //make sure the file is opened without errors before we try to gather information about it
              if (file[i - 1].is_open())
              {
            //gather the file name
                     tmp_filename = argv[i];
 
                     //initialize the number of lines in the file
                     lines.push_back(0);
 
                     //isolate the file name from the file loaded and store it in our file name vector
                     fileName.push_back(tmp_filename.substr(tmp_filename.find_last_of("\\") + 1, tmp_filename.length()));
 
                     //now gather the amount of lines in that file
                     while (!file[i - 1].eof())
                     {
                            //temporary buffer for gathering the current line in the file
                            string buffer;
                getline(file[i - 1], buffer);
                //we subtract 1 from the index of the line numbers because our loop starts and 1
                            lines[i - 1]++;
                     }
              }
 
              //close the file so we can open it under a new file directory next itation
              file[i - 1].close();
       }
 
       //-----------------------------------------------
 
       //make sure we have files that were loaded before displaying output
       if (lines.size() != 0 && fileName.size() != 0)
       {
              //add our 2 beginning caption headers
              output.push_back(""), output.push_back(""), output.push_back("");
 
              //display the chart header and captions
              if (column1Caption.length() < column1Size)
              {
            output[0] = output[0] + column1Caption;
                     for (unsigned int a = 0; a < (column1Size - column1Caption.length()); a++) output[0] = output[0] + " ";
              }
              else
              {
                     output[0] = output[0] + column1Caption.substr(0, column1Size);
              }
              if (column1Caption.length() < column2Size)
              {
            output[0] = output[0] + "| " + column2Caption;
              }
              else
              {
                     output[0] = output[0] + "| " + column2Caption.substr(0, column2Size);
              }
 
              //draw our seperator between the captions
              for (unsigned int a = 0; a < (column1Size + column2Size); a++) output[1] = output[1] + "-";
 
              //add a space between our seperator and our first file name
              for (unsigned int a = 0; a < column1Size; a++) output[2] = output[2] + " ";
              output[2] = output[2] + "|";
 
              //loop through each file name and store it in the chart along with the number of lines in that file
              for (unsigned int i = 0; i < fileName.size(); i++)
              {
                     //a buffer string to store our line data in
                     string line_buffer = "";
 
                     //add our data to our line buffer
                     if (fileName[i].length() < column1Size)
                     {
                line_buffer = line_buffer + fileName[i];
                            for (unsigned int a = 0; a < (column1Size - fileName[i].length()); a++) line_buffer = line_buffer + " ";
                     }
                     else
                     {
                            line_buffer = line_buffer + fileName[i].substr(0, column1Size);
                     }
 
                        //display the number of lines in the file
            line_buffer = line_buffer + "| " + intToStr(lines[i]);
 
                     //add our new line data to our output vector
                     output.push_back(line_buffer);
 
                     //add a new line to between now and the next file
                     line_buffer = "";
                     for (unsigned int a = 0; a < column1Size; a++) line_buffer = line_buffer + " ";
                     line_buffer = line_buffer + "|";
                     output.push_back(line_buffer);
              }
 
              //add the total amount in each file at the end of the chart if there is more than 1 file
              if (argc > 2)
              {
            //we re-declare our line buffer because it was only in scope in the for... loop)
               string line_buffer = "";
               //total amount of lines in all of the files
               int totalLines = 0;
                     //final column message (related to the total number of lines in all of the files
                     string final_message = "Total Lines:";
 
                     //add a space between the last file name
                     output.push_back(""), output.push_back("");
 
                     //calculate the total amount of lines in all of the files
                     for (unsigned int n = 0; n < lines.size(); n++) totalLines = totalLines + lines[n];
 
                     //add the final message to the end of the chart
                     line_buffer = line_buffer + final_message + " : " + intToStr(totalLines);
 
                     //add the new output to the chart
                     output.push_back(line_buffer);
              }
 
              //-----------------------------------------------
 
                //display our output data
              for (unsigned int i = 0; i < output.size(); i++)
              {
                     cout << output[i] << endl;
              }
 
              //add one final space to the end of the chart
              cout << endl;
              for (unsigned int a = 0; a < (column1Size + column2Size); a++) cout << "-";
              cout << endl << endl;
 
              //-----------------------------------------------
 
              //ask the user if they would like to save the output data to a text file
              while (true)
              {
                     //display the message whether or not to save the output to a text file
                     cout << "Would you like to save the results to a text file? (y/n):  ";
 
                     //user's selection on what to do (save output to file or exit)
                     char userSelection = getch();
 
                     //the user has selected "yes"
                     if (tolower(userSelection) == 'y')
                     {
                            cout << endl << endl
                                    << "Please enter a filename:  ";
 
                            //get the file name from the user and save the file
                            string fileDirectory = argv[0];
                            tmp_filename = "";
                            getline(cin, tmp_filename);
                            ofstream outputFile;
                            outputFile.open((fileDirectory.substr(0, fileDirectory.find_last_of("\\") + 1) + tmp_filename + ".txt").c_str());
 
                            //add the output to the file
                            if (outputFile.is_open())
                            {
                                   for (unsigned int i = 0; i < output.size(); i++)
                                   {
                                          outputFile << output[i] << "\n";
                                   }
                            }
 
                            //close the output file
                            outputFile.close();
 
                            //exit out of the program
                            break;
                     }
                     //the user has selected "no"
                     else if (tolower(userSelection) == 'n')
                     {
                //exit out of the program
                            break;
                     }
                     //the user has entered an invalid option
                     else
                     {
                            //clear the screen of any input
                            system("cls");
 
                            //display our output data
                            for (unsigned int i = 0; i < output.size(); i++)
                            {
                                   cout << output[i] << endl;
                            }
 
                            //add one final space to the end of the chart
                            cout << endl;
                            for (unsigned int a = 0; a < (column1Size + column2Size); a++) cout << "-";
                            cout << endl << endl;
 
                            //notify the user of the invalid option
                            cout << "\a"
                                    << "You have entered an invalid selection, please choose again"
                                    << endl << endl;
                     }
              }
       }
 
       return 0;
}
//----------------------------------------------------------------------------------------------------------------------------------------------