Okay, this is what I meant earlier. It's a development of the function from the last post:
Code:
void for_example (char *string) {
char *tok = strtok(string," "),
array[10][32], buffer[1024];
int c = 0, i;
FILE *in = fopen("test.c","r");
/* populate "array" */
while (tok) {
printf("->%s<-\n", tok);
if (strcmp(tok,"search") == 0) {
tok = strtok(NULL," ");
continue;
}
strcpy(array[c++],tok);
if (c==10) break; // max terms
tok = strtok(NULL," ");
}
/* scan file */
while (fgets(buffer,1024,in)) {
for (i=0; i<c ;i++) {
if (strstr(buffer,array[i])) {
printf("%s",buffer);
break; // don't reprint lines
}
}
}
fclose(in);
}
"test.c" is the source code itself, and the rest is the same as before. So when I run this:
./a.out search str tok arg
I get:
Code:
->search<-
->str<-
->tok<-
->arg<-
#include <string.h>
void for_example (char *string) {
char *tok = strtok(string," "),
while (tok) {
printf("->%s<-\n", tok);
if (strcmp(tok,"search") == 0) {
tok = strtok(NULL," ");
strcpy(array[c++],tok);
tok = strtok(NULL," ");
if (strstr(buffer,array[i])) {
int main(int argc, const char *argv[]) {
if (argc < 2) {
puts("No arg!");
for (i=1;i<argc;i++) {
strcat(input,argv[i]);
input[strlen(input)] = ' ';
Only the lines with "str", "tok", or "arg" in them The only problem with this is you can't search for "search".