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