[英]Why does _popen work here, but boost::process does not?
我在Windows上使用_popen擁有以下工作代碼,
m_pGNUPlot = _popen("/gnuplot/bin/gnuplot.exe", "w");
fprintf(m_pGNUPlot, "set term win\n");
fprintf(m_pGNUPlot, "set term pngcairo\n");
fprintf(m_pGNUPlot, "plot \"\Data.txt\" using 1:2 notitle\n");
fprintf(m_pGNUPlot, "set output \"\Out.png\"\n");
fprintf(m_pGNUPlot, "replot\n");
fflush(m_pGNUPlot);
但是問題是cmd窗口不斷彈出,並且沒有辦法阻止( Link )所以,我在boost :: process中編寫了等效的代碼
bp::pipe m_Write;
bp::environment env = boost::this_process::environment();
m_Plot = new bp::child("/gnuplot/bin/gnuplot.exe", bp::std_in < m_Write, env, boost::process::windows::hide);
m_Write.write("set term win\n", sizeof(char)*14);
m_Write.write("set term pngcairo\n", sizeof(char) * 19);
m_Write("plot \"\Data.txt\" using 1:2 notitle\n", sizeof(char)*35);
m_Write("set output \"\Out.png\"\n", sizeof(char)*22);
m_Write.write("replot\n", sizeof(char) * 8);
所以,我的問題是-兩個代碼段是否等效? 如果是這樣,為什么第二個不起作用?
我沒有Windows,因此我在Linux盒子上對其進行了測試,略有簡化:
#include <boost/process.hpp>
#include <iostream>
namespace bp = boost::process;
int main() {
bp::opstream m_Write;
boost::filesystem::path program("/usr/bin/gnuplot");
bp::child m_Plot(program, bp::std_in = m_Write);
m_Write << "set term png\n";
m_Write << "set output \"Out.png\"\n";
m_Write << "plot \"Data.txt\" using 1:2 notitle\n";
m_Write.flush();
m_Write.pipe().close();
m_Plot.wait();
std::cout << "Done, exit code: " << m_Plot.exit_code() << "\n";
}
打印:
Done, exit code: 0
並通過簡單的數據創建了這張漂亮的圖片:
在Windows上,利用Boost Filesystem path
功能執行以下操作:
boost::filesystem::path program("C:\\gnuplot\\bin\\gnuplot.exe");
如果整個腳本確實是固定的,請考慮使用原始文字:
m_Write << R"(set term png
set output "Out.png"
plot "Data.txt" using 1:2 notitle)" << std::flush;
m_Write.pipe().close();
是的,謝謝你! Boost功能強大,但是由於缺少教程和示例,因此很難入門。
是的,所以對我來說最后的工作代碼是-
bp::opstream m_Write; //output stream to pipe
boost::filesystem::path program("/gnuplot/bin/gnuplot.exe");
m_Plot = new bp::child(program, bp::std_in = m_Write, bp::windows::hide); //this solves the problem with _popen
m_Write << "set term png\n";
m_Write << "set term pngcairo\n";
m_Write << "set output \"" + ToPosixPath(sPath) + "\"\n"; //Notice how this works with std::string :)
m_Write << "plot \"" + CreateTemp(X, Y) + "\" using 1:2 notitle\n";
m_Write << "exit\n";
m_Write.flush();
m_Write.pipe().close();
m_Plot->wait(); //boost doc states "The call to wait is necessary, to obtain it and tell the operating system, that no one is waiting for the process anymore."
delete m_Plot;
一些要點-
在Windows中,支持gnuplot.exe本身的管道的exe,而在Linux中,有兩個-gnuplot.exe和pgnuplot.exe。
確保在GUI中測試您的腳本,此代碼會以靜默方式失敗! 返回碼將為0。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.