Friday, 24 November 2017

C++ Program to Implement the Program Used in grep/egrep/fgrep


Code:

#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:

Enter file name : prog.cpp
Enter the pattern: while

#include#includeint 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



More C++ Programs:














100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...