| //--------------------------- |
| //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; |
| } |
| //---------------------------------------------------------------------------------------------------------------------------------------------- |