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 -
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;
}

Dani AI

Generated

Nice catch by and well done for fixing it — the bug was in the inner-loop initialization. The outer loop advances i over s, but the inner loop always started comparing at a fixed position (you had j = 1), so the code only ever matched when the pattern happened to sit at that single offset. Initializing the inner index from the current outer index (i.e. start comparing at i) fixes the logic.

A few follow-up points to make this more robust and easier to maintain:

  • Simpler: use the standard library. Replace the custom strindex with strstr for clarity and fewer edge cases:

    if (strstr(line, pattern))
        printf("%s", line);
  • Reading lines: your getline (K&R style) is fine for learning, but rename it (e.g. my_getline) to avoid clashing with POSIX getline. Consider fgets or a dynamic buffer if you expect lines longer than MAXLINE.

  • File handling: instead of relying on shell redirection, treat files explicitly (use fopen with argv) or be sure to use the correct redirection syntax in Windows:
    program < "D:\count.txt" > "D:\output.txt"

  • Edge cases and behavior: decide whether search should be case-insensitive, whether an empty pattern should match, and how to treat CRLF newlines (trim them before matching if needed). Also beware returning found from main — nonzero return codes usually indicate failure to the shell; print the count and return 0 on success if that matters.

  • Debugging: compile with warnings (-Wall -Wextra), test with small inputs that exercise pattern at index 0, 1, end-of-line, and overlapping occurrences, and add temporary debug prints of indices if a match still looks wrong.

These small changes will make the utility more predictable and easier to extend.

Recommended Answers

All 2 Replies

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.

commented: OMG, Thank you! +2

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.