bluepeople12
bluepeople12

Reputation: 11

Grep not found when call execl

I have here some code in C++. I want to execute the program grep in linux. When compiling, there are no errors.

com.append("grep");
execl(com.c_str(), "-n", "-w", word.c_str(), list_files.at(i + 1).c_str(), NULL);

But when I execute my c++-program, there were nothing. Then I wanted to find the error with errno.

The result was 2 and I know that this means that he couldn't find this program.

But what should I do so I can start grep from my c++-program ?

Upvotes: 1

Views: 397

Answers (2)

sehe
sehe

Reputation: 392931

Perhaps the binary isn't found.

  • check what com contains before appending (it should contain the proper path, like /usr/bin/, with a trailing path-separator...)

  • try with "/usr/bin/grep" instead

Upvotes: 0

Keith Thompson
Keith Thompson

Reputation: 263237

execl() doesn't search $PATH for the executable. Either give it the full path to the grep command, or use execlp() which does search $PATH.

man execl for more information.

Upvotes: 6

Related Questions