【问题标题】:How to get string in system()如何在 system() 中获取字符串
【发布时间】:2021-12-24 17:45:09
【问题描述】:

我知道我可以看到他们

std::cout << system("my cmd");

但我想获得 cout 流。 我怎样才能得到它们?

std::string s; or char s[200];
s << system("my cmd");

它没有用。显示invalid operands to binary expression ('char [200]' and 'int')

【问题讨论】:

  • std::cout &lt;&lt; system("my cmd"); 将打印来自std::system(这是一个int)的返回值,而不是来自您在system(cmd); 中执行的命令的stdout。在 C++ 中没有从外部命令捕获输出的标准方法。如果需要,您可以使用boost::process 或自行处理特定于平台的 API。
  • std::cout &lt;&lt; system("my cmd"); 等价于int result = system("my cmd"); std::cout &lt;&lt; result;"my cmd" 的输出无论如何都会出现。
  • 枫问:有进展吗?请询问您是否未能得到我提出的boost 建议。

标签: c++ stream std system


【解决方案1】:

std::cout &lt;&lt; system("my cmd"); 将打印来自std::system(这是一个int)的返回值,而不是来自您在system("my_cmd"); 中执行的命令的stdout。在 C++ 中没有捕获外部命令输出的标准方法。

我建议安装boost 库(如果尚未安装)并使用boost::process,这样可以轻松完成。

同步示例:

#include "boost/process.hpp"

#include <iostream>
#include <string>

namespace bp = boost::process;

int main() {
    // Use boost's pipes to capture the output:
    // https://www.boost.org/doc/libs/release/doc/html/boost/process/basic_ipstream.html
    bp::ipstream out;
    bp::ipstream err;

    // boost's version of system() has poor documentation. Read the
    // tutorial in the top link instead.
    bp::system("/usr/bin/ls", "-m", "non-existing", "/dev",
               bp::std_out > out, bp::std_err > err, bp::std_in < stdin);

    // note how you can redirect stdout, stderr and even stdin to/from boost's ipstream's

    // Now you can read from the ipstreams with std::getline, just like any
    // other istream:
    std::string line;
    std::cout << "stdout capture:\n";
    while(std::getline(out, line)) {
        std::cout << line << '\n';
    }

    std::cout << "\nstderr capture:\n";
    while(std::getline(err, line)) {
        std::cout << line << '\n';
    }
}

可能的输出:

stdout capture:
/dev:
autofs, block, bsg, btrfs-control, bus, cdrom, char, console, core, cpu,
cpu_dma_latency, disk, dma_heap, dri, fb0, fd, full, fuse, gpmctl, hidraw0,
hidraw1, hidraw2, hidraw3, hidraw4, hidraw5, hidraw6, hpet, hugepages, hwrng,
initctl, input, ipmi0, kmsg, kvm, log, loop0, loop-control, lp0, lp1, lp2, lp3,
mapper, mcelog, megaraid_sas_ioctl_node, mem, mqueue, net, null, nvram, port,
ppp, ptmx, ptp0, ptp1, pts, random, raw, rfkill, rtc, rtc0, sda, sda1, sda2,
sda3, sda4, sg0, sg1, shm, snapshot, snd, sr0, stderr, stdin, stdout, tty, tty0,
tty1, tty10, tty11, tty12, tty13, tty14, tty15, tty16, tty17, tty18, tty19,
tty2, tty20, tty21, tty22, tty23, tty24, tty25, tty26, tty27, tty28, tty29,
tty3, tty30, tty31, tty32, tty33, tty34, tty35, tty36, tty37, tty38, tty39,
tty4, tty40, tty41, tty42, tty43, tty44, tty45, tty46, tty47, tty48, tty49,
tty5, tty50, tty51, tty52, tty53, tty54, tty55, tty56, tty57, tty58, tty59,
tty6, tty60, tty61, tty62, tty63, tty7, tty8, tty9, ttyS0, ttyS1, ttyS10,
ttyS11, ttyS12, ttyS13, ttyS14, ttyS15, ttyS16, ttyS17, ttyS18, ttyS19, ttyS2,
ttyS20, ttyS21, ttyS22, ttyS23, ttyS24, ttyS25, ttyS26, ttyS27, ttyS28, ttyS29,
ttyS3, ttyS30, ttyS31, ttyS4, ttyS5, ttyS6, ttyS7, ttyS8, ttyS9, udmabuf, uhid,
uinput, urandom, usb, usbmon0, usbmon1, usbmon2, vcs, vcs1, vcs2, vcs3, vcs4,
vcs5, vcs6, vcsa, vcsa1, vcsa2, vcsa3, vcsa4, vcsa5, vcsa6, vcsu, vcsu1, vcsu2,
vcsu3, vcsu4, vcsu5, vcsu6, vfio, vga_arbiter, vhci, vhost-net, vhost-vsock,
watchdog, watchdog0, zero

stderr capture:
/usr/bin/ls: cannot access 'non-existing': No such file or directory

【讨论】:

  • 谢谢,所以我的代码只是 cout system() 函数的 return(int) ,system() 仍然将它的结果打印到控制台。我误以为是cout的输出。
  • @MapleQ 是的,没错。您是否设法使用boost::process::system 捕获外部命令的输出,就像我在他的回答中显示的那样?
  • 是的!我使用了 boost::process,它起作用了。谢谢!
  • @MapleQ 太棒了!请考虑accepting 有帮助的答案。
猜你喜欢
  • 2014-04-10
  • 2010-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-09
  • 1970-01-01
相关资源
最近更新 更多