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.
If your are on windows then you need to do the following
- Create a Pipe1 using CreatePipe api of windows. Use this pipe for reading data from STDOUT of the child process.
- Create a Pipe2 the same way and use that pipe for writing data to the STDIN of the child process.
- 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.
- Close the write end of Pipe1 and read end of Pipe2.
- 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.
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.
Data that one program prints to standard output (std::cout
in C++) can be piped to the standard input (std::cin
) of another program. The specifics of how the two programs are connected depends on the environment (specifically operating system and shell).
You can create a class that holds your data (with >>
and <<
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:
echo "4 1.1 2.2 3.3 4.4" | ./a.out
Not sure how to do pipes on Windows though...