This is a C++ Program to implement grep linux command.
Here is source code of the C++ Program to Implement the Program Used in grep/egrep/fgrep. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
#include<stdio.h>
#include<dirent.h>
int main()
{
char fn[10], pat[10], temp[200];
FILE *fp;
printf("\n Enter file name : ");
scanf("%s", fn);
printf("Enter the pattern: ");
scanf("%s", pat);
fp = fopen(fn, "r");
while (!feof(fp))
{
fgets(temp, sizeof(fp), fp);
if (strcmp(temp, pat))
printf("%s", temp);
}
fclose(fp);
return 1;
}
Output:
$ g++ grep.cpp $ a.out Enter file name : prog.cpp Enter the pattern: while #include<stdio.h>#include<dirent.h>int main(){ char fn[10], pat[10], temp[200]; FILE *fp; printf("\n Enter file name : "); scanf("%s", fn); printf("Enter the pattern: "); scanf("%s", pat); fp = fopen(fn, "r"); while (!feof(fp)) { fgets(temp, sizeof(fp), fp); if (strcmp(temp, pat)) printf("%s", temp); } fclose(fp); return 1;}} ------------------ (program exited with code: 0) Press return to continue
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
Here’s the list of Best Books in C++ Programming, Data Structures and Algorithms.
If you find any mistake above, kindly email to [email protected]Related Posts:
- Practice Programming MCQs
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Apply for C++ Internship
- Check Programming Books