简体   繁体   English

如何获取一个程序的输出并将其用作C ++上另一个程序的输入?

[英]How do I take the output of one program and use it as the input of another on C++?

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 示例: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. 所以,我需要在我的程序中调用这个程序并分析这个数字序列。

Data that one program prints to standard output ( std::cout in C++) can be piped to the standard input ( std::cin ) of another program. 一个程序打印到标准输出(C ++中的std::cout )的数据可以通过管道传输到另一个程序的标准输入( std::cin )。 The specifics of how the two programs are connected depends on the environment (specifically operating system and shell). 两个程序如何连接的细节取决于环境(特别是操作系统和shell)。

If your are on windows then you need to do the following 如果您在Windows上,那么您需要执行以下操作

  1. Create a Pipe1 using CreatePipe api of windows. 使用Windows的CreatePipe api创建Pipe1。 Use this pipe for reading data from STDOUT of the child process. 使用此管道从子进程的STDOUT读取数据。
  2. Create a Pipe2 the same way and use that pipe for writing data to the STDIN of the child process. 以相同的方式创建Pipe2并使用该管道将数据写入子进程的STDIN。
  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. 还传递cmd行参数。
  4. Close the write end of Pipe1 and read end of Pipe2. 关闭Pipe1的写入端并读取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. 您可以通过读取Pipe1直接从子进程输出中读取数据。

For a sample have a look at the following link. 有关示例,请查看以下链接。 http://msdn.microsoft.com/en-us/library/ms682499%28VS.85%29.aspx http://msdn.microsoft.com/en-us/library/ms682499%28VS.85%29.aspx

Hope this is what you are looking for. 希望这是你正在寻找的。

You can create a class that holds your data (with >> and << overloads) 您可以创建一个保存数据的类(使用>><< overloads)

include <iostream>
#include <iterator>
#include <vector>
class MyData
{
public:
  friend 
  std::istream& 
  operator>>(std::istream& in, MyData& data)
  { 
    in >> data.size ;
    data.m_data.resize(data.size);
    std::copy( 
          std::istream_iterator<float>(in), 
          std::istream_iterator<float>( ),
          data.m_data.begin()
           );
  }
  friend
  std::ostream&
  operator<<(std::ostream& out, MyData& data)
  { 
    out<<  data.size << " ";
    for(size_t i=0;i<data.size;++i){
      out<< data.m_data[i] <<" ";
    }
    return out;
  }
private:
  int size;
  std::vector<float> m_data;
};

And then you can call it like so 然后你就可以这样称呼它

int
main  (int ac, char **av)
{
  MyData d;
  std::cin>>d; //input one set of data;

  //test
  std::cout<<d;

  //get multiple data files
  std::vector<MyData> buffer;

   std::copy( 
            std::istream_iterator<MyData>(std::cin), 
            std::istream_iterator<MyData>( ),
        std::back_inserter(buffer)); // copies all data into buffer

}

On Linux the test pipe can be formed like so: 在Linux上,测试管道可以这样形成:

echo "4 1.1 2.2 3.3 4.4" | ./a.out

Not sure how to do pipes on Windows though... 不知道怎么在Windows上做管道...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将C ++程序输出作为Java输入 - Take C++ program output as Java input 一个可执行文件的“连续” C ++输出作为另一个程序的输入 - 'Continuous' C++ output of one executable as Input to another program 我如何编程当前程序以将命令输入到C ++中的另一个控制台中? - How do i program my current program to input a command into another console in C++? 如何使用 C++ 程序截取 FSX 的屏幕截图? - How do I take a screenshot of FSX using a C++ Program? 如何使用Python从C++程序中输入并获取output? - How to use Python to input to and get output from C++ program? 如何将使用 curses/ncurses 的 ac 程序的输出传递给另一个程序? - How do I pass the output of a c program which use curses/ncurses to another program? 如何从C ++程序运行另一个程序 - How do I run another program from a C++ program 我如何从他们的全名的用户那里得到输入,然后 output 他们的名字和姓氏分开? (C++) - How do I take input from the user of their full name, and then output their first name and last name seperately? (C++) C ++ Boost :: serialization:如何在一个程序中归档对象,然后在另一个程序中恢复它? - C++ Boost::serialization : How do I archive an object in one program and restore it in another? 如何将文件用作现有C ++程序的输入流? - How do I use file as input stream to existing C++ program?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM