Jul 28, 2012 at 11:10am Jul 28, 2012 at 11:10am UTC
Suppose a function prints out the numbers : 1 2, 3, 4, 5, 6
is there any methods to store the output numbers into the new array?
Jul 28, 2012 at 11:19am Jul 28, 2012 at 11:19am UTC
Here is how to store the first in an array. I'll leave the rest to you.
newArray[0] = 1;
Not brilliantly helpful, I know, but your question is so vague that it's impossible to answer any more specifically.
Last edited on Jul 28, 2012 at 11:29am Jul 28, 2012 at 11:29am UTC
Jul 28, 2012 at 11:46am Jul 28, 2012 at 11:46am UTC
@ Moschops: thanks for the reply but it's not helpful
Jul 28, 2012 at 12:01pm Jul 28, 2012 at 12:01pm UTC
As helpful as your original question.
I'm going to guess that your function outputs to cout. In the absence of information, I have to guess. You can replace the streambuf associated with cout. Then, you can get the data from that buffer. In this example, the function outputting the number 1 2 3 4 5 6 is in the middle. Instead of going to screen, the output is put into the buffer, which is then dumped to a string and after cout is restored, output to screen. You could easily put it into an array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#include <iostream>
#include <ostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
stringstream replacementStream;
// replace the cout buffer with your redirected buffer :
streambuf *oldbuf = cout.rdbuf(replacementStream.rdbuf());
{ // Placeholder for a function sending values to cout
cout << 1 << 2 << 3 << 4 << 5 << 6;
}
string line;
getline(replacementStream, line);
// put cout back
cout.rdbuf(oldbuf);
cout << line;
}
Last edited on Jul 28, 2012 at 12:06pm Jul 28, 2012 at 12:06pm UTC
Jul 29, 2012 at 12:22am Jul 29, 2012 at 12:22am UTC
@Moschops: Thanks for your reply.. I already tackle this another way. Hope I'll use your idea next time.