lab10exF.c
/* lab10exF.c: solution to an ENCM 339 Fall 2005 exercise */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE_LENGTH 80 /* Does not include '\n' or '\0'. */
#define LINE_ARRAY_SIZE (MAX_LINE_LENGTH + 2)
void die(const char *msg);
/* REQUIRES: msg points to beginning of a string.
* PROMISES: string is output to stderr, then program is terminated
* with exit(1).
*/
int main(int argc, char **argv)
{
FILE *fp;
char line[LINE_ARRAY_SIZE];
int line_number = 0;
if (argc != 3)
die("Number of arguments after program name must be two.\n");
fp = fopen(argv[2], "r");
if (fp == NULL)
die("Could not open input file.\n");
while(fgets(line, LINE_ARRAY_SIZE, fp) != NULL) {
line_number++;
if (strstr(line, argv[1]) != NULL)
printf("%d:%s", line_number, line);
}
fclose(fp);
return 0;
}
void die(const char *msg)
{
fputs(msg, stderr);
exit(1);
}
Generated by GNU enscript 1.6.1.