Hey i have a program that reads the contents of a file into an array...now i want to extend this program so that when i type in search "word" it prints out the line in the text that has the "word in it.
The code i have for opening the file and just printing it is:
Code:
#include <stdio.h>
#include <string.h>
#define SIZE 100
int main(int argc, char *argv[]) {
FILE *input;
char run[1000];
int count;
int i=0;
char string[1000][SIZE];
input=fopen(argv[1], "r");
if(input==NULL) {
printf("No such file\n");
} else {
while((fgets(string[i], SIZE, input)!=NULL)) {
i++;
}
count=i;
while (strcmp(run, "quit")!=0){
printf("Enter command > ");
scanf("%s", run);
if (strcmp(run, "print")==0){
for(i=0;i<count-1;i++){
printf("%d %s", i, string[i]);
}
}
}
}
return 0;
}
this is what i want to extend my program into:
For example the file has these lines:
1 I love programming
2 I hate programming
3 Programming runs the world
then in my program i type in "search love"
the program should print:
1 I love programming
I am thinking that i need to store the word i am searching for into a temp array and then used the strcmp function to compare this "word" with all the ones in the file and then printing out the required lines...I do not know if this logic if right..and even if its i usually have trouble converting the logic into C syntax..
Any help will be greatly appreciated..