如何获取一个程序的输出并将其用作另一个在C ++上的输入? [英] How do I take the output of one program and use it as the input of another on C++?

查看:593
本文介绍了如何获取一个程序的输出并将其用作另一个在C ++上的输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,将实验的计数作为命令字符串参数,并输出浮点数序列。
示例:
im_7.exe 10
10.41
13.33
8.806
14.95
15.55
13.88
10.13
12.22
9.09
10.45

I have a program that takes the counts of experiments as command string argument and outputs the sequence of floating numbers. Example: im_7.exe 10 10.41 13.33 8.806 14.95 15.55 13.88 10.13 12.22 9.09 10.45

所以,我需要在我的程序中调用这个程序并分析这个数字序列。

So, i need to call this program in my program and analyze this sequence of numbers.

推荐答案

如果您在Windows上,则需要执行以下操作

If your are on windows then you need to do the following


  1. 使用CreatePipe API创建一个Pipe1。

  2. 使用相同的方式创建Pipe2,并使用该管道将数据写入子进程的STDIN。

  3. 创建子进程,在启动信息中提供这些句柄并继承父进程的句柄。

  4. 关闭Pipe1的写入结束并读取Pipe2的结尾。

  5. 在您的情况下,子进程输入。您可以直接从子流程输出中读取数据,方法是从Pipe1中读取。

  1. Create a Pipe1 using CreatePipe api of windows. Use this pipe for reading data from STDOUT of the child process.
  2. Create a Pipe2 the same way and use that pipe for writing data to the STDIN of the child process.
  3. Create the child process and in the startup info provide these handles and inherit the handles from the parent process. Also pass the cmd line arguments.
  4. Close the write end of Pipe1 and read end of Pipe2.
  5. In your case you are not writing anything into the child process input. You can straight away read the data from the child process output by reading from the Pipe1.

对于示例,请查看以下内容链接。 http://msdn.microsoft.com/en-us /library/ms682499%28VS.85%29.aspx

For a sample have a look at the following link. http://msdn.microsoft.com/en-us/library/ms682499%28VS.85%29.aspx

希望这是您要查找的内容。

Hope this is what you are looking for.

这篇关于如何获取一个程序的输出并将其用作另一个在C ++上的输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆