Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我这里有一些 C++ 代码。我想在linux中执行程序grep。编译时,没有错误。
com.append("grep"); execl(com.c_str(), "-n", "-w", word.c_str(), list_files.at(i + 1).c_str(), NULL);
但是当我执行我的 c++ 程序时,什么都没有。然后我想用errno找到错误。
结果是 2,我知道这意味着他找不到这个程序。
但是我应该怎么做才能从我的 c++-program 开始 grep ?
execl()不搜索$PATH可执行文件。要么给它grep命令的完整路径,要么使用execlp()which does search $PATH。
execl()
$PATH
grep
execlp()
man execl了解更多信息。
man execl
也许找不到二进制文件。
在附加之前检查 com 包含的内容(它应该包含正确的路径,例如/usr/bin/,带有尾随路径分隔符...)
/usr/bin/
尝试使用“/usr/bin/grep”代替