Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program (C++) that allows the user to determine how many lines, words an

ID: 3562906 • Letter: W

Question

Write a program (C++) that allows the user to determine how many lines, words and characters are in a file
specified by the user. The program expects the user to pass information about what file to read and
exactly what to count via a command line argument. The program is invoked via a command of the form
at DOS


hw4 filename /options


where options is a combination of characters "l" (lines), "w" (words), "c" (chars). For example
hw4 words.1 {default, reports char, word and line count for the file words.1}
hw4 words.2 /cw { reports char and word count for file words.2}
hw4 words.2 /x { Error: program sends error message to screen }


If the user uses incorrect command syntax, for example, if the character immediately following the "/" is
not a "c", "w" or "l", an error message should be displayed that explains the command line syntax. There
should be more than one type of error message.


Hints and Suggestions and Warnings
1) For the purpose of this assignment, a word is a contiguous string of non-whitespace characters
delimited by whitespace characters. Don't forget about isspace. Note that whitespace characters are
characters, so you need to count them as well.


2) Program will only deal with file input via command line as argument to program. Keyboard input will
not work, must have at least two arguments on command line. Also DOS redirection of output will work.


3) One reason for this assignment is to provide some experience with C++ structs. Structs work well for
this assignment, since there are so many pieces of information to keep track of. Need to use following
structs for full credit.


struct CmdLineRecord
{
int SyntaxError; // True if user syntax is incorrect
int WantsLineCount; // Set to true if user wants line count
int WantsWordCount; // Set to true if user wants word count
int WantsCharCount; // Set to true if user wants char count
};
struct CountsRecord
{
unsigned LineCount;
unsigned WordCount;
unsigned CharCount;
};


4) A working shell has been created for you, thanks to me. It is found on my website and is called
"hw4shell.cpp". Make good use of it, it will run the way it is.


5) Output should be tested with the files of words.1, words.2, words.3, words.4 and words.5, which are
the same files used in homework #1.


6) You can use command line arguments from inside the IDE. See the handouts on Command Line
Arguments in C++ 2008.NET. If you are using 6.0 I have a had out for that as well.


7) Screen output can be captured as usual or use DOS redirection to a file, then print the file.

Explanation / Answer

// WordCount.cpp : Defines the entry point for the console application.

//

#include <iostream>

#include <fstream>

bool GetWordCount2(const char *srcFileName, int &numChars, int &w, int &numLines)

{

    FILE *istream = fopen(srcFileName, "rb");

    if (istream == 0)

        return false;

   c = 0;

    w = 0;

   l = 0;

    const int len = 4096;

    char buf[4096];

    do

    {

        if( feof(istream))

            break;

        int nBytesRead = fread(buf, 1, len, istream);

        if(nBytesRead <= 0)

            break;

        int nc, wc, lc;

        GetWordCount(buf, nBytesRead, nc, wc, lc);

       c += nc;

        w += wc;

       l += lc;

    } while(1);

    fclose(istream);

    return true;

}

bool GetWordCount(const char *buf, int len, int &numChars, int &w, int &numLines)

{

   c = len;

    w = 0;

   l = 0;

    int prevId = -1;

    for(int i = 0; i < len; i++)

    {

        if(buf[i] == ' ')

           l++;

        // If you want, you can include comma (,) or any

        // other characters that can separate words

        if(buf[i] == ' ' || buf[i] == ' ' ||

            buf[i] == ' ' || buf[i] == ' ')

        {

            // To Skip continuos white space character.

            // You can comment this if block, if you do not to count

            // successive white space character for one word.

            if(prevId == i - 1)

            {

                prevId = i;

                continue;

            }

            prevId = i;

            w++;

        }

    }

    return true;

}

bool GetWordCount(const char *srcFileName, int &numChars, int &w, int &numLines)

{

   c = 0;

    w = 0;

   l = 0;

    std::ifstream ifs(srcFileName, std::ios::binary | std::ios::in);

    if(ifs.is_open() == false)

        return false;

    const int len = 4096;

    char buf[4096];

    while(1)

    {

        if(ifs.eof())

            break;

        ifs.read(buf, len);

        int nBytesRead = ifs.gcount();

        if(nBytesRead <= 0)

            break;

        int nc, wc, lc;

        GetWordCount(buf, nBytesRead, nc, wc, lc);

       c += nc;

        w += wc;

       l += lc;

    }

    ifs.close();

    return true;

}

int main()

{

    int nc, wc, lc;

    GetWordCount("hw4 words.1", nc, wc, lc);

    std::cout << "Chars: " << nc << " ";

    std::cout << "Words: " << wc << " ";

    std::cout << "Lines: " << lc << " ";

    return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote