First version due: Fri. Jan 23, 11:59pm.
Second version due: Fri. Jan 30 11:59pm
This is a simple program that you should be able to do if you are well prepared for this course.
Write a program that takes two arguments on the command line. The first should be a string (call it str) and the second the name of a file. Your program should print every line of the given file that contains str as a substring. Each line must be numbered, starting with line 1. For example, if your executable program is called find, the command
./find fish myfile.txtwill print each line in file myfile.txt that contains "fish". The output might be as follows.
3. the terns eat fish that they catch 10. fishermen see the terns migratingNote that the string being sought is not required to be a full word. It can be part of a word, for example.
This program must be written according to the following list of requirements.
This program must be written in two modules. One module, which must be called find.cpp, should contain the main program. The other module, which must be called search.cpp, should contain a function that takes four parameters: (1) an array pattern of characters telling the string to search for; (2) an integer indicating the logical size of pattern. (3) an array text of characters to search; and (4) an integer indicating the logical size of text; (The logical size is the number of characters in the string.) That function should produce a boolean result: true if the search string occurs in the array to search, and false if not. The heading for the search function must be
bool occurs(char pattern[], int patternSize, char text[], int textSize)There must be a third file, search.h, that contains a prototype for the function that is exported by search.cpp. Both find.cpp and search.cpp must include search.h. File search.h must not contain prototypes or comments about any functions other than occurs. Module find.cpp must not include search.cpp.
The main program must use the function from the other module to check a line of text. It cannot do the check directly itself.
File search.h must have a clear and concise contract describing what your occurs function does. The contract must clearly describe the role of each parameter, and the meaning of the result. The contract must not describe irrelevant things, such as the method used, or other details of the function body.
File main.cpp must contain a comment describing what the program does.
If you write any helper functions, then each one must have a clear contract describing what it does.
The standard library contains a function called strstr that checks to see whether one string is contained in another. For this assignment you may not use that function, or any similar function. Do you own search. You may use other library functions such as strlen.
You must turn in a Makefile that will build your program. It should build an executable file called find. Your makefile must be called Makefile.
Your program must write its output to the standard output (cout).
Both modules must be instrumented with debug prints as follows. In the main program, provide an option to print each line of text as it is read. Then show the search string and whether the search string was found for that line. The search.cpp module must have an option to print the text and the pattern, and either the index where the string was found, or a report that it was not found.
Every debug print must be clearly labeled by the name of the function that printed it, and what is being printed. Raw debug prints are unacceptable.
The debug prints must be protected by #ifdef's of the following forms. In find.cpp:
# ifdef DEBUGFIND ... (debug prints here) # endifIn search.cpp:
# ifdef DEBUGSEARCH ... (debug prints here) # endifso that they can be turned on by setting DEBUGFIND and DEBUGSEARCH and turned off by not setting them.
Your program must not defined DEBUGFIND or DEBUGSEARCH. That is for me to do. But before turning in your program you should try defining DEBUGFIND and DEBUGSEARCH to see that things work. You can do that as follows. In find.cpp:
#define DEBUGFINDIn search.cpp:
#define DEBUGSEARCH
If the file to be searched does not exist, your program must print a message that the file does not exist, and immediatly stop.
Your program must not use any global variables. All communication between functions must be by parameter passing.
Your program must be well indented and readable.
To turn in this program, log into one of the Unix computers in the lab. (You can log in remotely.) Ensure that your files are there. Change to the directory that contains those files. Then issue command
~karl/3510/bin/submit 1a find.cpp search.cpp search.h Makefileto submit version 1, and
~karl/3510/bin/submit 1b find.cpp search.cpp search.h Makefileto submit version 2. You should receive immediate feedback on your submission. If you have any trouble, please let me know.
The main program should perform a loop.
linenumber = 1.
Loop
Read a line into an array. Determine how many characters are in the line.
If there are no characters in the line
If the end of file was reached then exit the loop.
else
If the pattern occurs in the line then print the line, numbered by linenumber.
Add 1 to linenumber
End loop
The search function should also contain a loop
k = 0
While there are still enough characters left in the text array from index k to the end do
If the pattern occurs in the text at index k
return true
Add 1 to k.
End loop
You might find it useful to write a function to determine whether the pattern occurs at a given index in the text.
You can get the command line parameters as follows. The heading for the main function should be
int main(int argc, char* argv[])Then the pattern is argv[1] and the file to read is argv[2]. Each is an array of characters that ends on a 'null character' (a character whose code is 0). For example, to open the file, you can write
ifstream inf(argv[2]);
Test your program. In particular, make sure that it works correctly when the pattern occurs in the first and last lines of the file. Make sure that it does something sensible when the file that it is supposed to search does not exist.
Q. Is it acceptable for me to use the stdio input and output functions instead of the iostream ones?
A. Yes.
Q. Does it matter whether I open the file in text mode or binary mode?
A. Open it in text mode. On Unix, there is no difference between the two, but it will matter on Windows.
Q. Are there other details of the program that have not been spelled out in the hints?
A. Yes.
Q. Does my program have to work correctly if the search string occurs at the end of a line?
A. Yes.
Q. I do not have time to learn how to use Unix. Is it ok if I just email my program to you instead of following the instructions for handing it in?
A. No.
Q. I do not want to use the command line. Is it ok if I just make my program search a file whose name is "myfile.txt"?
A. No.
Q. I do not want to use the command line. Is it ok if I just make my program ask the user for the name of the file to search, and for the string to search for?
A. No.
Q. I do not understand modules. Is it ok if I put everything in one module?
A. No.
Q. I do not see how to combine the modules without including search.cpp in find.cpp. Is it ok if I just do that?
A. No.
Q. Is it ok if I ignore the names that you have given, and choose different names for my modules?
A. No. By the way, SEARCH.CPP and search.cpp are not the same name.
Q. I do not understand makefiles. Is it ok if I don't turn one in?
A. No.
Q. I do not understand contracts, and I do not really like writing comments. Is it ok if I don't write a contract?
A. No.
Q. I do not understand instrumenting programs. Is it ok if I do not instrument the programs?
A. No.
Q. I do not understand the #ifdef lines. Is it ok if I make my program so that it always does the debug prints?
A. No.
Q. I am pretty sure that the file being searched will exist. Is it ok if my program does not bother to check whether the file exists?
A. No.
Q. Is it ok if I use different parameters to the occurs function?
A. No.
Q. I have some nice ideas on making this program better, but that requires that it work a little differently from what you have asked for. Is it ok for me to implement something different from what you have requested?
A. No.
Q. Will I get any credit if I turn in a program that does not compile?
A. No.
Q. Is my program expected to work correctly, according to the requirements stated here?
A. Yes.