Assignment 4: A Little Language for Text File Statistics¶
In this assignment your task is to implement a small programming language that
calculates statistics about text files. The C++ program you write will read
and execute the commands from a text file called commands.txt
.
Each command in commands.txt
is written on a single line, and consists of
two strings in this form:
command filename
There must be at least one space (and maybe more) between command
and
filename
. Some of the lines might be blank, and those lines should be
ignored.
There are three different commands: num_chars
, num_lines
, and
num_words
. They each cause messages to be printed to cout
as
follows:
num_chars filename
prints a message tocout
indicating the number of characters infilename
.num_lines filename
prints a message tocout
indicating the number of newlines ('\n'
characters) infilename
.num_words filename
prints a message tocout
indicating the number of words infilename
. A word is defined as the string that an input stream (such ascin
) returns when reading a string value.
Case matters, and so, for instance, Num_chars
is not a valid command
name.
Sample Output¶
Here’s a sample commands.txt
file:
num_lines output.log
num_chars story.txt
num_words story.txt
num_lines file_list.txt
Assuming your program is named a4
, and that the files output.log
,
story.txt
, and file_list.txt
are in the same directory as a4
, the
correct output is this:
$ ./a4
output.log has 0 lines
story.txt has 42 characters
story.txt has 9 words
file_list.txt has 8 lines
The file output.log
is an empty file.
The file story.txt
contains this:
Once upon
a time
a princess ate an
apple.
Note that the character count for story.txt
is 42. This includes the
newline characters and spaces. There is one space character immediately after
the word “an”.
The file file_list.txt
contains this:
directory:
index.html
toc.html
logs:
access.log
errors.log
sys.log
The exact files are contained in the folder a4_sample.zip
.
Error Handling¶
An important part of this assignment is dealing with errors. In particular:
If
commands.txt
doesn’t exist in the same folder as your program, then stop the program and print a helpful error message indicatingcommands.txt
couldn’t be found. Don’t usecmpt::error
.It’s possible that
commands.txt
could be empty. In that case, stop the program and print a friendly message such as “commands.txt is empty” and then end normally. Don’t usecmpt::error
.If a line begins with a string that is not a valid command, then print a friendlt and helpful error message saying that the command was not recognized (include the name of the unrecognized command in the error message). Don’t stop the program. Instead, skip the line with the invalid command and go on to the next line.
If the
filename
for a command does not name a file in the same directory as your program, then print a friendly and helpful error message such as “Cannot find file: filename” (of course, replacefilename
with the name of the file that wasn’t found). After the message is printed, the program should skip that line and go on to the next line. Don’t stop the program usingcmpt::error
.If a line starts with a valid command, but has no filename, or more than one filename, then print a friendly and helpful error message. For example, both of these lines should cause errors:
num_lines num_chars story.txt file_list.txt
After the message is printed, the program should skip that line and go on to the next line. Don’t use
cmpt::error
.There can be one, or more, spaces after a command. Also, extra whitespace characters are permitted before or after any of the lines in
commands.txt
. Whitespace characters include spaces, newlines (i.e. blank lines), tabs, etc. So, for example, thiscommands.txt
file generates exactly the same output as the neatly-formatted one shown above:num_lines output.log num_chars story.txt num_words story.txt num_lines file_list.txt
Note that the only spaces are allowed between a command and a file name. You cannot, for example, put a newline immediately after a command.
Required Functions¶
While there are no specific required functions for this assignment, we want to see that your program uses functions in a smart way.
Use functions to divide-up your program into logical chunks. Avoid long stretches of code; a good general rule of thumb is that a function should be no more than about 10 lines long.
Bonus Marks¶
You can earn up to 2 bonus marks for correctly implementing one, or both, of the following features:
(1 bonus mark) Allow
//
-style comments incommands.txt
. They should work like C++’s//
-style comments: everything after the//
up to the end of the line is ignored. For example:// num_lines output.log num_chars story.txt // print stats for num_words story.txt // story.txt num_lines file_list.txt
(1 bonus mark) Allow more than 1 file name to appear after any command. Multiple names are separated by 1, or more, spaces. For example:
num_chars story.txt file_list.txt
This prints the same thing as these two separate commands:
num_chars story.txt num_chars file_list.txt
If you implement this feature, then it is no longer an error if a line contains more than one file name.
These features are optional: you can still get a 100% by implementing the rest of the assignment correctly.
Submission¶
Put your program in a single file called a4.cpp
. It should #include
,
at most, these files: iostream
, fstream
, cmpt_error.h
, and
string
. Do not #include
any other files.
When it’s ready to be submitted, compress a4.cpp
into a zip-file archive
called a4_submit.zip
by typing the following command in a Linux console
window:
$ zip a4_submit.zip a4.cpp
Don’t type the $
— that’s the command prompt. Do not compress your
program in any other way — only .zip
please!
Include the name of the course (CMPT 130), the semester (Fall 2017), and your name and student number in comments at the top of the file.
If you received help from any other person, or book (such as the textbook), or website, etc., then you must cite this help in a comment at the top of your program. If you do not include such a comment, we will assume the program is entirely your own work. Not citing help (even if you accidentally forget) is considered academic dishonesty and will be dealt with seriously.