#include<stdio.h>
#include<string.h>
int main(int argv,char *args[])
{
FILE *f;
char str[100];
char c;
char word[100];
int i,flag,j,m,k;
char ar[]="hi";
char temp[30];
if(argv<4)
{
printf("usage: grep <pattern> <filename>\n\n");
return;
}
f=fopen(args[3],"r");
while(!feof(f))
{
i=0;
while(1)
{
fscanf(f,"%c",&c);
if(feof(f))
{
str[i++]='\0';
break;
}
if(c=='\n')
{
str[i++]='\0';
break;
}
str[i++]=c;
}
if(strlen(str)>=strlen(args[2]))
for(k=0;k<=strlen(str)-strlen(args[2]);k++)
{
for(m=0;m<strlen(args[2]);m++)
{
temp[m]=str[k+m];
}
temp[m]='\0';
if(strcmp(temp,args[2])==0)
printf("%s\n",str);
break;
}
}
return 0;
}
Output:
[telnet50@linux ~]$ ./a.out
usage: grep <pattern> <filename>
[telnet50@linux ~]$ grep int grepdemo.c
int main(int argv,char *args[])
int i,flag,j,m,k;
printf("usage: grep <pattern> <filename>\n\n");
printf("%s\n",str);
No comments:
Post a Comment