gpt4 book ai didi

c++ - 在 C++ 中使用 popen 执行 perl 命令时出错

转载 作者:行者123 更新时间:2023-11-30 04:11:44 25 4
gpt4 key购买 nike

在我的 C++ 程序中,我想执行一个 perl 命令并读取执行返回的输出。我为此使用 popen,但在执行命令时出现错误:

命令:

string cmd = "perl -ne 's/^\\S+\\s//; if ((/" +
pattern1+ " START/ .. /" + pattern2+ " END/) && /find/)"
" { print \"$_\"}' file";
stream = popen(cmd.c_str(),"r");

如果我在命令行中执行此命令它会工作,但在 C++ 中我会收到此错误:

Search pattern not terminated at -e line 1.

在命令行中工作的命令是,在 C++ 中我已经转义了 '\' 和 '"':

perl -ne 's/^\\S+\\s//; if ((/aaa START/ .. /bbb END/) && /find/) { print "$_"}' file

如果我执行这个命令,它会起作用:“perl -ne print $_ file”。但是我的初始命令没有。我做错了什么。谢谢。

最佳答案

这是你的转义符 \。当 \\ 变成 \ 时,您必须在 C++ 字符串中将它们加倍。然后 shell 会按照您在命令行上看到的那样进行处理。即另一轮 \\ 变成了 \

关于c++ - 在 C++ 中使用 popen 执行 perl 命令时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20118076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com