[英]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上,那么您需要执行以下操作
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.