Write a program that allows the user to determine how many lines, words and char
ID: 3623420 • Letter: W
Question
Write a program 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 DOShw4 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- characters 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;
};
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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.