Is there a way I can rewrite the following code which reproduce the functionality of the grep command in linux, in less lines of code? Or is there something I can make to save some memory or yo make the code even more efficient?
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <error.h>
#include "ourhdr.h"
#define size 1024
int main(int argc, char **argv)
{
char fis[100],car[10],buff[size];
FILE *file;
if(argc!=3)
{
printf("Not all parameters were given <./a.out> <char> <numefisier>\n");
}
else
{
file=fopen(argv[2],"r");
if (file==NULL)
{
err_ret("Fisierul sursa nu exista %s", argv[2]);
exit(0);
}
strcpy(fis,argv[2]);
strcpy(car,argv[1]);
while((!feof(file)))
{
while(fgets(buff,size,file)!=NULL)
{
if(strstr(buff,car))
printf("%s \n",buff);
}
}
fclose(file);
}
return 0;
}