i'm writing a program that takes a string and list of files and prints all the lines that contain the string. i'm having problems with my findString method. i did some debugging and it turns out when i call strcmp on strings that are the same it returns -1 or 1. does anyone know what i need to fix?

#include <stdio.h>
#include <string.h>


int inputLength = 500;


int findString(char *str, char *line) {
char *s;
s = strtok(line," ");
while (s != NULL) {
if (!strcmp(str,s))
return 1;
else
s = strtok(NULL, " ");
}
return 0;
}


int printFile(int argc, char *argv[], char *str) {
FILE *f;
char s[inputLength];
int k;
for (k = 2; k < argc; k++) {
f = fopen(argv[k],"r");
if (f) {
while (fgets(s,inputLength,f) != NULL) {
if (findString(str, s) == 1)
printf("%s %s", argv[k], s);
}
}
}
fclose(f);
return 0;
}


int main(int argc, char *argv[]) {
char * str = argv[1];
printFile(argc, argv, str);
return 0;
}

Several problems here

fgets() returns the line feed character into its argument, so you must overwrite it if you want to compare

you should use strstr() and not strcmp() since you are comparing whole lines read from your files

here is a working solution

#include <stdio.h>
#include <string.h>

int inputLength = 500;

int findString(char *str, char *line) {
        char *s;
        s = strtok(line, ".");
        while (s != NULL) {
                if (!strstr(str,s))
                        return 1;
                else
                        s = strtok(NULL, " ");
        }
        return 0;
}

int printFile(int argc, char *argv[], char *str) {
        FILE *f;
        char s[inputLength];
        int k;
        int i;
        for (k = 2; k < argc; k++) {
                f = fopen(argv[k],"r");
                if (f) {
                        while (fgets(s, inputLength, f) && !feof(f)) {
                                for (i=0; i<inputLength && s[i] != 0xA; i++);
                                s[i]='\0';
                                if (findString(str, s) == 1)
                                        printf("%s %s", argv[k], s);
                        }
                }
        }
        fclose(f);
        return 0;
}

int main(int argc, char *argv[]) {
        char * str = argv[1];
        printFile(argc, argv, str);
        return 0;
}

A slight improvement

Rather than search for "." with strtok(), search for (char)0x200 - the last character that is not end of line.
You can use strstr() to search for lines with common words on the same line or strcmp() to compare the whole lines. You could choose either with a switch on the command line. Meantime this works with the following invocation format

progname SearchString f1 f2

#include <stdio.h>
#include <string.h>

int inputLength = 500;

int findString(char *str, char *line) {
        char *s;
        s = strtok(line, (char)0x200);
        printf("\nfindString(): str==<%s>", str);
        printf("\nfindString(): s==<%s>", s);
        while (s != NULL) {
                if (!strcmp(str,s))
                        return 1;
                else
                        s = strtok(NULL, " ");
        }
        return 0;
}

int printFile(int argc, char *argv[], char *str) {
        FILE *f;
        char s[inputLength];
        int k;
        int i;
        for (k = 2; k < argc; k++) {
                f = fopen(argv[k],"r");
                if (f) {
                        while (fgets(s, inputLength, f) && !feof(f)) {
                                for (i=0; i<inputLength && s[i] != 0xA; i++);
                                s[i]='\0';
                                if (findString(str, s) == 1)
                                        printf("%s %s", argv[k], s);
                        }
                }
        }
        fclose(f);
        return 0;
}

int main(int argc, char *argv[]) {
        char * str = argv[1];
        printFile(argc, argv, str);
        return 0;
}
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.