My program compiles and runs, but does not print any output. It is supposed to read the text file and output lines that contain the pattern (which this in case is Bob - so it should print Bobby, Bobbie, etc if those names are in the text file)..

This is my command argument - <"D:\count.txt">"D:\output.txt"
Here is the copy of count.txt - http://dl.dropbox.com/u/20491067/count.txt
output.txt is blank txt file that copies patterns from count.txt..

I read the file from count.txt and output to output.txt

------------------------------------

#include <stdio.h>

#define MAXLINE 1000 // maximum input line length

int getline(char line[], int max);
int strindex(char source[], char searchfor[]);

char pattern[MAXLINE] = "Bob"; // pattern to search for

// find all lines matching pattern
int main() {
	char line[MAXLINE];
	int found = 0;

	while (getline(line, MAXLINE) > 0)
		if (strindex(line, pattern) >= 0) {
			printf("%s", line);
			found++;
		}
	return found;
}

// get line into s, return length
int getline(char s[], int lim) {
	int c, i;
	
	i = 0;
	while (--lim > 0 && (c=getchar()) != EOF && c != '\n')
		s[i++] = c;
	if (c == '\n')
		s[i++] = c;
	s[i] = '\0';
	return i;
}

// return index of t in s, -1 if none
int strindex(char s[], char t[]) {
	int i, j, k;

	for (i = 0; s[i] != '\0'; i++) {
		for (j=1, k=0; t[k]!='\0' && s[j]==t[k]; j++, k++)
			;
		if (k > 0 && t[k] == '\0')
			return i;
	}
	return -1;
}

At line 41, you always comparing t against s[1...], no matter how deep you are in the enclosing loop. If the string has a pattern at position 1, you'll match it many timers. Otherwise, you'll never match it.
In other words, initialization j=1 is wrong.

Comments
OMG, Thank you!

At line 41, you always comparing t against s[1...], no matter how deep you are in the enclosing loop. If the string has a pattern at position 1, you'll match it many timers. Otherwise, you'll never match it.
In other words, initialization j=1 is wrong.

Oh my.. I must have been sleeping while coding.. it should have been j=i, not j=1...

It works now. Thanks!

This question has already been answered. Start a new discussion instead.