&1”; bp::ipstream out; std::字符串行; bp::子c(cmd,bp::std_out>out); //while应该读取输出的每一行 //但是这里没有执行 while(c.running()&&std::getline(out,line)和&!line.empty()) { if(line.find(p_name)!=std::string::npos) { 返回true; } } c、 等待(); 返回false; },c++,boost,c++17,C++,Boost,C++17" /> &1”; bp::ipstream out; std::字符串行; bp::子c(cmd,bp::std_out>out); //while应该读取输出的每一行 //但是这里没有执行 while(c.running()&&std::getline(out,line)和&!line.empty()) { if(line.find(p_name)!=std::string::npos) { 返回true; } } c、 等待(); 返回false; },c++,boost,c++17,C++,Boost,C++17" />

如何使用c++;libboost? 我试图运行一个外部shell命令,使用C++的Boost库读取它的输出,但是看起来命令不是在运行,或者是我不能访问输出。我举了一个例子,写了以下内容: #include <boost/process.hpp> namespace bp = boost::process; bool is_process_running(std::string p_name){ string cmd = "ps aux 2>&1"; bp::ipstream out; std::string line; bp::child c(cmd, bp::std_out > out); // the while is supposed to read each line of the output // but the execution doesn't enter here while(c.running() && std::getline(out, line) && !line.empty()) { if(line.find(p_name) != std::string::npos) { return true; } } c.wait(); return false; } #包括 名称空间bp=boost::process; bool正在运行进程(std::string p\u name){ string cmd=“ps aux 2>&1”; bp::ipstream out; std::字符串行; bp::子c(cmd,bp::std_out>out); //while应该读取输出的每一行 //但是这里没有执行 while(c.running()&&std::getline(out,line)和&!line.empty()) { if(line.find(p_name)!=std::string::npos) { 返回true; } } c、 等待(); 返回false; }

如何使用c++;libboost? 我试图运行一个外部shell命令,使用C++的Boost库读取它的输出,但是看起来命令不是在运行,或者是我不能访问输出。我举了一个例子,写了以下内容: #include <boost/process.hpp> namespace bp = boost::process; bool is_process_running(std::string p_name){ string cmd = "ps aux 2>&1"; bp::ipstream out; std::string line; bp::child c(cmd, bp::std_out > out); // the while is supposed to read each line of the output // but the execution doesn't enter here while(c.running() && std::getline(out, line) && !line.empty()) { if(line.find(p_name) != std::string::npos) { return true; } } c.wait(); return false; } #包括 名称空间bp=boost::process; bool正在运行进程(std::string p\u name){ string cmd=“ps aux 2>&1”; bp::ipstream out; std::字符串行; bp::子c(cmd,bp::std_out>out); //while应该读取输出的每一行 //但是这里没有执行 while(c.running()&&std::getline(out,line)和&!line.empty()) { if(line.find(p_name)!=std::string::npos) { 返回true; } } c、 等待(); 返回false; },c++,boost,c++17,C++,Boost,C++17,目标是验证ps aux的每一行输出,并搜索进程是否正在运行。那么,这里有什么问题?或者,您可以提供一个简单的代码片段来完成此操作吗?我遇到了这个问题。。。我只能使用boost::asio使流程正常工作 下面是代码,希望能有所帮助。下面的代码处理所有三个子进程的流 唯一的外部字符串是exename,而tstring是std::basic\u字符串 void UBC::Run( const tstring和args, 常量std::字符串和输入, std::字符串和输出, std::字符串和错误)

目标是验证
ps aux
的每一行输出,并搜索进程是否正在运行。那么,这里有什么问题?或者,您可以提供一个简单的代码片段来完成此操作吗?

我遇到了这个问题。。。我只能使用boost::asio使流程正常工作

下面是代码,希望能有所帮助。下面的代码处理所有三个子进程的流

唯一的外部字符串是exename,而tstring是std::basic\u字符串

void UBC::Run(
const tstring和args,
常量std::字符串和输入,
std::字符串和输出,
std::字符串和错误)
{
使用名称空间boost;
asio::io_服务ios;
标准::矢量输出(128 piperr,
工艺::标准_in
只需使用shell(或使用
bp::system
):

#include <boost/process.hpp>
namespace bp = boost::process;

bool is_process_running(std::string p_name){
    std::vector<std::string> args { "-c", "ps aux 2>&1" };
    bp::ipstream out;
    bp::child c(bp::search_path("sh"), args, bp::std_out > out);

    for (std::string line; c.running() && std::getline(out, line);) {
        if (line.find(p_name) != std::string::npos) {
            return true;
        }
    }
    c.wait();

    return false;
}

#include <iostream>
int main() {
    std::cout << "bogus: " << is_process_running("bogus") << "\n";
    std::cout << "a.out: " << is_process_running("a.out") << "\n";
}

请更具体地说明输出内容以及您期望的输出内容。你怎么知道“这里没有执行”?你已经使用调试器了吗?是的,我正在调试。关键是,它没有进入while循环,根据文档中的示例,这就是我如何通过lineAnswer从
cmd
执行行获取输出,以解决您的问题。例如,您可以执行
ps aux | grep PROC_NAME | grep-v“grep.*PROC_NAME”
。boost文档是否告诉您它将启动shell?如果没有,您就不能使用像
2>&1
这样的shell语法。原则上这过于复杂了。当输入/输出是真正异步的并且需要混合时,可能需要它——即,如果在进程需要输入时保持同步读取,则会发生死锁。示例中的命令不接受任何输入(事实上可以
bp::std_in.close()
bp::std_in
),而且,
tstring(exeName)+\u T(“”+args
也不会像广告那样工作。这是真正的工作代码吗?这看起来很可疑。这就是我如何让它工作的,在像OP一样转了几个小时后。我确实需要所有三条流。这是一个加密实用程序。这是生产代码,不是示例。但是,
bp::child
不可能只接受由空格分隔的参数。那是什么版本的boost?它是未经修改的吗?(如果这样做有效的话,它要么是一个奇怪的bug,要么曾经是一个奇怪的bug)@sehe:
tstring(exeName+T(“”)+args
就像广告一样工作,因为exeName是一个
const-TCHAR*
。它也可以作为
tstring{}+exeName+\T(“”)工作+args
,但不是没有对
tstring
的引用。但是为什么呢?为什么不仅仅是
…,bp::std\u out>out,bp::std\u err>out)
?@n.m.Mmm。这可能真的管用。但我不认为这是问题的本质。另外,它的“语义”可能与shell重定向不同,具体取决于它的实现方式。我想说,出于这个原因,提出的问题是有价值的,你的评论是一个值得欢迎的补充。
#include <boost/process.hpp>
namespace bp = boost::process;

bool is_process_running(std::string p_name){
    std::vector<std::string> args { "-c", "ps aux 2>&1" };
    bp::ipstream out;
    bp::child c(bp::search_path("sh"), args, bp::std_out > out);

    for (std::string line; c.running() && std::getline(out, line);) {
        if (line.find(p_name) != std::string::npos) {
            return true;
        }
    }
    c.wait();

    return false;
}

#include <iostream>
int main() {
    std::cout << "bogus: " << is_process_running("bogus") << "\n";
    std::cout << "a.out: " << is_process_running("a.out") << "\n";
}
bogus: 0
a.out: 1