Thread: Code...
View Single Post
Old 11-06-2005, 07:06 AM   #4 (permalink)
tgo
Registered User
 
Join Date: Jul 2005
Posts: 185
OS: slackware 10.1


Send a message via AIM to tgo
I liked your idea and heres what I came up with for it:

Usage:
Code:
tgo@localhost:~/c$ cat test
askdfkl
asdfasdf
BOB
dsf
BOB
JOE
TIM
BOB
tgo@localhost:~/c$ gcc -o search search.c
tgo@localhost:~/c$ ./search
Usage: search <file> <text>
tgo@localhost:~/c$ ./search test BOB
Match found at line: 3
Match found at line: 5
Match found at line: 8
tgo@localhost:~/c$
Code:
Code:
#include <stdio.h>
#include <string.h>

int main(int argc,char *argv[])
{
        FILE *file;
        char cur[80];
        int x;
        x = 1;

        if (argc != 3)
        {
                printf("Usage: search <file> <text>\n");
                exit(1);
        }

        if ((file = fopen(argv[1],"r")) == NULL)
        {
                printf("Error opening file.\n");
                exit(1);
        }

        while (fgets(cur, 80, file))
        {
                if(strstr(cur,argv[2]))
                {
                printf("Match found at line: %d\n",x);
                }
                x++;
        }

        return 0;
}
__________________
My new homepage:
tgo is offline   Reply With Quote