首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用C++在命令提示符中输入多个命令或循环执行一个命令?

如何使用C++在命令提示符中输入多个命令或循环执行一个命令?
EN

Stack Overflow用户
提问于 2018-07-26 05:37:27
回答 1查看 345关注 0票数 0

我正在尝试实时显示命令的结果。我已经能够检索结果,但我想要一个实况流。每当我在代码中放入while循环来重复该命令时,就会打开一个窗口,执行该命令,然后关闭该窗口,然后打开另一个窗口,重复该过程,直到程序崩溃。我希望命令提示符在接收命令时在后台运行,因为我在它前面运行的是一个QT gui。

下面是我目前的代码:

cmd = _popen("snmpget -v 2c -c public 192.168.127.101 .1.3.6.1.4.1.8691.8.4.6.1.1.3.1.1.4.1", "r"); //input command in format (snmp_function -v version -c community_string IP_address OID)
if (cmd == NULL)
{
    perror("_popen");
    exit(EXIT_FAILURE);
}
while (fgets(result, sizeof(result), cmd))          //gets full responce string to snmpget and passes string to result variable
{
    std::string str = result;                       //sets string to values in variable result
    std::regex rgx("\"([^\"]*)\"");                 //retrieves only the section of the output string that is between double air quotes
    std::smatch match;
    std::string buffer;
    std::stringstream ss(str);
    std::vector<std::string> strings;
                                                //necessary? whitespace
    while (ss >> buffer)
        strings.push_back(buffer);
    for (auto& i : strings)
    {
        if (std::regex_match(i, match, rgx))
        {
            std::ssub_match submatch = match[1];
            std::string str = submatch.str();
            for (size_t j=0; j<str.length(); j++)
            {
                output[j] = str[j];
            }
        }
    }
}

_pclose(cmd);

std::string只是为了得到我想要的那部分返回值。我通常可以通过在while循环的末尾放入一个getchar()来保持窗口打开,但我不希望依赖于用户按enter来获得更新的结果。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51528047

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档