C Search Program - TechRepublic
Question
June 2, 2009 at 03:16 AM
samaeel

C Search Program

by samaeel . Updated 17 years, 1 month ago

Implement the console filter MYFIND.EXE (MYFIND.C)
which : Reads lines from an input file and writes to the output only those containing the substring passed as passing parameter(Parameter #1) to the filter.

MYFIND must work properly as a filter.

However, if more than one passing parameters which are NOT switches (they don’t begin with a – ) are passed to it at the command line it must obtain them as

Input filename (Parameter #2)
Output filename (Parameter #3)

and open them respectively using their file descriptors in place of those of the redirectable standard devices (stdin,stdout)

In all cases :

? the first passing parameter is mandatory and represents the substring.
? the second passing parameter is a switch if it has one of the following values

a. -R reverse the condition (only lines NOT containing the substring)
b. -N put the input file’s line number as a prefix to the output line

If this first passing parameter has not been passed, then the program must send an error message to the standard error device.

Example:
Given file FRIENDS.TXT we wish to create a subfile which contains / does not contain only those lines containing the substring “Susan”.
The following command lines must all work properly

a. MYFIND “Susan” < FRIENDS.TXT > SUSAN.TXT
b. MYFIND “Susan” FRIENDS.TXT > SUSAN.TXT
c. MYFIND “Susan” FRIENDS.TXT SUSAN.TXT
d. MYFIND ?R ?N “Susan”
e. MYFIND ?R “Susan” FRIENDS.TXT OTHERS.TXT

HELP I : Use two file descriptors for instance fdin fdout
Based on the value of argc (1,2,3, .. ) check if the 2nd passing parameter is a switch and then given that there are filenames also at the command line, assign to the mentioned file descriptors a value of the appropriate standard device (stdin,stdout) or the return value of an fopen(..) statement.
Then, use (for either case above) the functions fgets fputs (or fprintf) to read or write from/to the files or devices.

HELP II: The function that tells us whether a substring is found inside a mainstring is
char *strstr(char *mainstring ,char *substring)

This discussion is locked

All Comments