Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

this program read and count only a latter from text file, but i need this program read and count each words from text file. can

this program read and count only a latter from text file, but i need this program read and count each words from text file. can you fix it please!

for example: if the text file is " I am cs 162 student. I also work at Downtown",

the out put would be:

I - 2 time

am - 1 time

cs - 1 time

162 - 1 time...........etc

The program is

main.cpp

#include "main.h" // header file for this module

// Function Definitions int main(int argc, char **argv)

// Stack variables int count_table[256] = {0}; // Table of character counts. // Subscript: ASCII character code char byte = '\0'; // Input buffer std::fstream file; // Input file stream std::string file_name; // Input file name

// Get the input file name if (argc < 2) { std::cout << "Enter input file name: "; std::getline(std::cin,file_name); } else { file_name = argv[1]; }

// Open the file stream file.exceptions(std::fstream::failbit); // Generate exception if error try { file.open(file_name, std::ios::in); // Open for input } catch (const std::exception & exception_object) { std::cout << "?Exception opening \"" << file_name << "\": " << exception_object.what() << std::endl; return 1; }

// Count characters while(file.peek() != EOF) { byte = file.get(); count_table[static_cast(byte)]++; }

file.clear(); file.close();

// Display character count for (int index = 0; index < 256; index++) { if (count_table[index] > 0) { std::cout << "Code " << std::setw(3) << std::setfill(' ') << index << " "; if (index > ' ') { std::cout << "('" << static_cast(index) << "')"; } else { std::cout << "(n/a)"; } std::cout << " count is: " << std::setw(3) << std::setfill(' ') << count_table[index] << std::endl; } }

// All done return 0; }

main.h

// System Libraries #include // Time utilities #include // Limits #include // Limits #include // Limits #include // Assertions #include // Exceptions #include // Char type data #include // C++ String class #include // Math routines #include // Standard streams #include // File streams #include // String streams #include // Formatting #include // Regular expressions

// Function Declarations int main(int, char**); // Main function

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Analysis Using SQL And Excel

Authors: Gordon S Linoff

2nd Edition

111902143X, 9781119021438

Students also viewed these Databases questions

Question

Question What are the advantages of a written bonus plan?

Answered: 1 week ago