Thursday, December 30, 2010

'grep' Command implementation in C.


#include
#include
main()
{
int i,j,n,counter=0;
char file[50],str[20],buf[20];
FILE *fp;
printf("\nEnter file name with path and string :");
gets(file);
gets(str);
puts(file);
puts(str);
fp=fopen(file,"r");
while(fgets(buf,sizeof(buf),fp)!='\0')
{
if(strstr(buf,str))
{
printf("\n");
puts(buf);
counter++;
}
}
printf("\nThe number of time string find is :%d\n",counter);
fclose(fp);
}