Intro to C #2: minigrep

Starter code: minigrep-package.tar
Upload solutions via Moodle as: minigrep.tar

Goals

Rubric

1 - author name(s) in comment at top of your C source file 6 - program correctness 3 - code quality (including thorough error handling)

grep

If you ever want to search for strings in one or a million files in a Unix environment, you should get to know grep.

Let me be more direct: you should get to know grep, period.

Here are some example grep commands:

grep moose myfile prints every line of myfile that contains the string "moose" grep -v moose myfile prints every line of myfile that does NOT contain the string "moose" grep -i moose myfile prints every line of myfile that contains "moose" case-insensitively ("Moose", "MoOsE", "mOOSe",...) grep -r moose mydirectory prints every line containing "moose" for every file in mydirectory or any of its descendant directories grep '^A' myfile prints every line starting with A grep 'B^' myfile prints every line ending with B grep 'moose.*goat' myfile prints every line containing "moose" followed eventually by "goat" etc.

Reading lines of text with fgets

For your previous assignment, you could do the whole thing reading one byte (i.e., one char) at a time from the input file. Thus, the standard library function fgetc was exactly what you needed.

But sometimes, you need a whole line of text. In those cases, fgets is often the right tool. Here's a quick sample of how to use it:

FILE *input_file = fopen(input_file_name, "r"); // ... skipping the error-handling code ... char buffer[100]; if (fgets(buffer, 100, input_file) != NULL) { // Do something with the null-terminated string now in buffer }

Read the description of fgets on its man page to answer questions like: does the newline character get stored in buffer? is the string in buffer always null-terminated? what happens if the line in the the file is longer than the size of the buffer?

Programming assignment #2: minigrep

For this assignment, you will write a C program minigrep.c that takes the name of an input file and a search string as command-line parameters (in that order) and prints to standard output every line of the input file that contains the search string, preceded by the (1-based) line number of the line.

For example, suppose you have this file named animals.txt:

moose coatimundi starling dingo parrot flamingo dorking chicken goat

Then running your minigrep program on this file should look like this:

$ ./minigrep animals.txt ing 3: starling 4: dingo 6: flamingo 7: dorking chicken

(When I went looking for animal examples for minigrep, I discovered for the first time, to my great delight, the existence of the dorking chicken. On the other hand, I'm sad that "dorking" here merely refers to the town of Dorking, England, rather than some sort of mysterious chicken activity.)

You may assume that no line of your file contains more than 200 bytes/chars, including the newline character.

Submitting your work

As you did for your first homework assignment, put all the files you want to submit in a folder named minigrep (this should probably just be a single source file minigrep.c), run tar cvf minigrep.tar minigrep, and then submit the tar file via Moodle.

Remember that advice from last time?

Have fun!

Keep experimenting! Keep asking questions!