Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions