Question: I need help to convert this program to c++ language #include #include #include #include int main(int argc, char const *argv[]) { char buf[1000]; //check for

I need help to convert this program to c++ language

#include

#include

#include

#include

int main(int argc, char const *argv[])

{

char buf[1000];

//check for arguments

if(argc!=3){

printf("usage: simple-grep ");

exit(0);

}

//Initializing file pointer

FILE *file;

//to check file exist or not

if (access(argv[1], F_OK) !=-1)

{

//to check it is readable or not

if(access(argv[1], R_OK !=01))

{

//opening file

file = fopen(argv[1], "r");

//reading file line by line

while (fgets(buf,sizeof(buf), file) !=NULL)

{

//ending the string by null

buf[strlen(buf) - 1] = '\0';

//strstr to check for the specific word in buffer

if (strstr(buf, argv[2])) {

printf("%s ", buf);

}

}

//close the file

fclose(file);

}

else{

printf("file is not having read permission");

exit(0);

}

}

else{

printf("file not found ");

exit(0);

}

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!