Corrections, clarifications, and other announcements regarding this programming assignment will be found below.
In this assignment you will write a program that searches for occurrences of given word within a given file. The word and the file name will be entered as command-line arguments.
The goals of this assignment are to give you experience:
All your code for this assignment should be placed in a file named findWord.cpp. Your findWord program will do the following:
You will need to use the iostream, fstream, string, and cstring libraries.
~cs368-1/public/html/assignments/p5/files/echoArgs.cpp
An example of how your findWord program should run and the output it should produce is given below. The example uses the file test.txt which contains the following:
The attitude that programmers should not have to pay for features that they do not use permeates the [C++] language. Another example will further illustrate this influence. Imagine that an integer variable holding a negative number is right-shifted by one location. What bit value should be moved into the topmost position? If we look at the machine level, architectural designers are divided on this issue. On some machines, a right shift will move a zero into the most significant bit position, while on other machines the sign bit (which, in the case of a negative number, will be 1) is extended. Either case can be simulated by the other, using software, by means of a combination of tests and masks. -- p. 4, "C++ for Java Programmers", Timothy Budd (Addison-Wesley, 1999)
The file test.txt can be found at:
~cs368-1/public/html/assignments/p5/files/test.txt
The following shows the compilation of the program as well as several runs. The user input at the command prompt is given in bold.
% g++ -g findWord.cpp -o findWord % findWord Proper usage: findWord <word> <file> where <word> is a sequence of non-whitespace characters <file> is the file in which to search for the word example: findWord the test.txt % findWord the test.txt Searching for 'the' in file 'test.txt' 2 : that they do not use permeates the [C++] language. Another example 3 : will further illustrate this influence. Imagine that an integer 5 : What bit value should be moved into the topmost position? If we 6 : look at the machine level, architectural designers are divided on 8 : the most significant bit position, while on other machines the sign 9 : bit (which, in the case of a negative number, will be 1) is extended. 10 : Either case can be simulated by the other, using software, by means # occurrences of 'the' = 13 % findWord C++ test.txt Searching for 'C++' in file 'test.txt' 2 : that they do not use permeates the [C++] language. Another example 13 : -- p. 4, "C++ for Java Programmers", Timothy Budd (Addison-Wesley, 1999) # occurrences of 'C++' = 2 % findWord summer test.txt Searching for 'summer' in file 'test.txt' # occurrences of 'summer' = 0 % findWord the noFile.txt File 'noFile.txt' could not be opened
Make sure your code follows the style and commenting standards used in CS 302 and CS 367. Note: the commenting standards use javadoc comments for class, method, and constructor headers. You do not need to use javadoc comments in your c++ programs for CS 368; however, your comments should include the same information as the javadoc comments. For example, your function header comments should include a description of what the function does, the name and a short description of each parameter, and a description of the return value.
Electronically submit the following file to your in "handin" directory by the due date and time (or refer to the late policy):
Please turn in only the files named above. Extra files clutter up the "handin" directories.
© 2012-2014 CS368 Instructors