CS 302/ECE 468 - Programming Assignment 1

In this assignment you will write a C filter program that processes input text by expanding macros found in the text. The values that the macros will expand to will come from either command line arguments or environment variables. This assignment makes use of files contained in this zip file.

Write a C program that reads from standard input and writes to standard output. Any character read from standard input that is not part of a macro should be written directly to standard output. When your program finds a macro in its input, it will "expand" (i.e., replace) the macro with a certain text string. In this assignment, a macro is defined to be a string of the form "#1#", or "#2#", or "#3#", etc., up to "#9#", that is, there are nine possible macros. When your program finds a macro string of the form "#n#" (where n is a single digit integer) and if n < argc, then your program should write to standard output, in place of the macro string, the n'th command line argument string. If n >= argc, then your program should use the Standard C Library function getenv() to see if there is an environment variable named #n# and if there is, use its string value to expand the macro. If there is no such environment variable, then your program should write the macro string unchanged to standard output.

In the zip file for this assignment you will find a sample text file that you can test your program with. Also in the zip file is a Windows batch file that automates the testing of your program with the sample input file (the batch file demonstrates how to set environment variables and how to use I/O redirection to process files). Another text file contains the correct output that your program should produce when the batch file is run.

Turn in a zip file containing your C program that solves this problem, the original sample input text file, the original batch file, and any other test files that you want to include. This assignment is due Monday, January 26.

Below are some references about I/O redirection, command line arguments, environment variables, and the getenv() function.

Return to the main homework page.
Return to the CS 302 home page.


compliments and criticisms