调用execl时找不到grep

1 c++ linux grep errno

我这里有一些C++代码.我想在linux中执行grep程序.编译时,没有错误.

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

但是当我执行我的c ++程序时,什么都没有.然后我想用errno找到错误.

结果是2,我知道这意味着他找不到这个程序.

但是我应该怎么做才能从我的c ++程序开始grep?

Kei*_*son 6

execl()不搜索$PATH可执行文件.要么给它grep命令的完整路径,要么使用execlp()哪个搜索$PATH.

man execl 欲获得更多信息.