Due: Wednesday, July 24th by 10:00 am; No late
exercises accepted.
Goals: Write a small program using the C++ STL map
container and learn how to use C++ streams to read data from a file.
Description: Write a C++ program that reads a text file whose filename is given as a single command-line argument. The program should read and count the individual words in the file, and, after reading the entire file, it should print a list of the words sorted by the words, and the number of times each word appears in the file. Each word should be written on a separate output line with the word appearing first followed by a single space and the number of occurrences.
For example, if the file quotes.txt
contains
then the output ofto be or not to be to do is to be to be is to do do be do be do
./ex12 quotes.txt
should begin with the following:
be 6 do 5 is 2 ...
You should make the following assumptions:
>>
operator to read the input file one
word (string) at a time. You may assume that each string read by
>>
is a "word" for the purposes of this exercise -- you
do not need to do any additional processing to handle
whitespace, punctuation, or similar issues.word
, Word
, and woRD
are three different words
because they contain different lower- and upper-case letters.<
) relation for strings.Hints: Take advantage of the C++ STL library; one of the map
containers should be particularly useful.
For reading files, take a look at the ifstream
class.
As usual, your code must:
g++ -Wall -g -std=c++17 -o ex12 ex12.cc
-- but
don't forget to submit a Makefile
.Makefile
, as described above, that compiles the code
with the g++
options -Wall -g -std=c++17
cpplint
may be helpful in flagging potential style
problems. (You can ignore cpplint
complaints about
using namespace
directives)
You should submit your exercise to the course Gradescope.