C Search Program

By Samaeel ·
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.

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

d. MYFIND ?R ?N "Susan"

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 conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Answers

Collapse -


by Bizzo In reply to C Search Program

Yes, that is the spec for a search program.

Sorry, but it's unlikely you'll get help for your entire coursework. Maybe you could start by getting some online resources on how to program in C, google bring this one up first, it might be worth trying.

Then, once you've tried to do your work, you may succeed, but you may get stuck. If you get stuck then you can come here with a specific question, and then I'm sure people will help.

<i>"What we have to learn to do, we learn by doing." </i>
- Aristotle

Collapse -

Totally agree

by Samaeel In reply to Correct

The problem is that i can't understand how to start it.. i dont want someone to give me the code. but a hint on how to start it. that s why i posted the whole code.. I dont understand how to use the parameters. i mean i understand how to use the substring if its provided but that as far as i can go atm.

Collapse -

Update! plz check this!!

by Samaeel In reply to C Search Program

#define MAX_LINE_LEN 4096

void cleanup(FILE* f1, FILE* f2) {
if (f1 != 0 && f1 != stdin) {
if (f2 != 0 && f2 != stdout) {

int main(int argc, char** argv) {

char* searchString = 0;

bool showMatches = true;
bool showLineNumbers = false;

FILE* input = stdin;
FILE* output = stdout;

if (argc == 1) {
fprintf(stderr, "missing arguments");
return 1;

searchString = argv[1];

for (int i = 2; i < argc; ++i) {
if (strcmp("-R", argv) == 0) {
showMatches = false;
} else if (strcmp("-N", argv) == 0) {
showLineNumbers = true;
} else {
if (i + 2 != argc) {
fprintf(stderr, "both input and output file must be specified");
return 2;

input = fopen(argv, "r");
output = fopen(argv, "w");

if (input == 0 || output == 0) {
fprintf(stderr, "couldn't open input or output file");
cleanup(input, output);
return 3;

char* line = char[MAX_LINE_LEN]; // handling arbitrarily sized lines makes this a ton more complicated

int lineNumber = 1;
while (fgets(line, MAX_LINE_LEN, input) != 0) {
bool foundSearchString = (strstr(line, seachString) != 0);
if (foundSearchString == showMatches) {
if (showLineNumbers) {
fprintf(output, "%D ", lineNumber);
fputs(output, line); // missing error handling

cleanup(input, output);
return 0;

Collapse -

My idea doesn't work..

by Samaeel In reply to C Search Program

Please need more info!

Related Discussions

Related Forums