Exercise 2
Makefiles and grep
Due Date - January 27, 2012 @ 2:00pm
Introduction
In Unix systems, there is a command called grep. Grep is a way to search for expressions in files. Grep has the form "grep <expression> <file to search>". For example, if there is a file called test1 that contains the the following line
The whole world loves it when you don't get down.
If you type "grep when test" in the command prompt, Unix will print "The whole world loves it when you don't get down." to the screen because the line contains the expression "when". Grep prints all lines when an instance of the expression occurs. Let there be a file test2 that contains the following
Penny Lane is in my ears and in my eyes
There beneath the blue suburban skies
I sit, and meanwhile back
If you type "grep ba test2", Unix will print
There beneath the blue suburban skies
I sit, and meanwhile back
This is back "surburban" in line 2 and "back" in line 3 contain the expression "ba"
Assignment
Write a C program that implments the grep and counts the number of instances that the expression appears in the file. The program should print all of the lines that contain the expression and the number of instances the expression appears in the file.
Output Code
You can download the code for the expected output here. The output code contains three functions.
PrintLine - Prints the line that contains the expression.
PrintCount - Prints the numbers of instances the expression appears in the file
PrintError - Prints the appropriate error messages if you have problems opening a file or there is the wrong number of command-line inputs
Specifications
-
You should print an error message and exit the program if the user does not give enough command line arguments. The expected output is in the output code
-
You should print an error message and exit the program if the user gives an invalid file. The expected output is in the output code.
-
The expression to search for and the name of file to be examined will be given as a command-line arguments.
-
Upper and lower case forms of the words should NOT be treated the same.
-
Numbers can be the string to be searched for.
-
Words will be no longer than 30 characters.
-
Lines in a file will be no longer than 100 characters.
-
The file will consist of only alphanumeric characters and the following punctuation marks: periods, question marks, exclamation points, commas, semi-colons, and colons
-
You CANNOT use the system function in your algorithm. Doing so will result in a deduction of 1 point from your grade.
Compile and Execute
How to compile and execute this program? Save this program into a file called ex1.c. In a shell, type
gcc -Wall -Wshadow ex2.c -o ex2
This means enabling all warning messages and detecting shadow variables. Warning messages are often indications of errors. For this assignment, you lose 0.1 point for each warning message. The command will generate an executable file called "ex2."
To execute this program, type
./ex2 <expression> <filename>
The second argument (argv[1]) is the expression you are searching for in the file and the third argument (argv[2]) is the file you will search.
Testcases
You can find a suite of testcases here. The file 'epxressions.dat' tell what expression was searched for in every example.
Submission Procedure
Create a zip file contain the folowing files
-
At least 1 .c and 1 .h file
-
Makefile - The makefile should produced the executable "ex2".
and then submit to the course grading website.
You need to provide a user name and a password through Blackboard before submission. The user name and the password are different from your Purdue career account.
.h Files
A .h file, short for header file, typically contains the following
-
All the the C library files that your program will need (i.e. stdio.h, stdlib.h, etc.)
-
All constatns you will need. Constants can be defined using the #define command. An example would be "#define MAXLEN 102".
-
All function headers for the function in your program.
Once completed, your .h file should be included in your .c file. This is done by typing the following: #include "filename.h"
An introduction about Makefile can be found here.
Grading
The maximum score for this exercise is 2.0 points.
Examples
Let's say an example file called example1.txt that contains the pharse "The man that eats the beans will suffer the most". The resulting output should be as followed
> ex2 the example1.txt
The man that eats the beans will suffer the most
Count: 2
Another example file is exampl2.txt that contains the following
When you are wanting in peace, the whole world is doing something absurd,
acting like a mad elephant, according to your judgement. But if you have
abundant peace within, then you will see that you have the capacity to transform
others' misdeeds. If you love humanity, then please acquire and develop inner peace.
This inner peace is bound to change the face of the world.
If one types ex2 s example2.txt, you would get the following output
When you are wanting in peace, the whole world is doing something absurd,
abundant peace within, then you will see that you have the capacity to transform
others' misdeeds. If you love humanity, then please acquire and develop inner peace.
This inner peace is bound to change the face of the world.
Count: 11
Turn on Warning Messages
You should use "gcc -Wall" instead of "gcc". This will turn on warning messages. Treat warning messages seriously because warnings are very likely errors.
|