Link to home
Start Free TrialLog in
Avatar of guildencrantz
guildencrantz

asked on

stdout to strstream?

I am currently trying to call an extrenal program from within my C++ code and then to collect the output and store it.

Currently I think that I want to store the information in a strstream.  Is there a way to redirect stdout to the strstream so that I can call system("XXX") and then have the output of XXX accessible through the strstream?

Or, is there a viable alternative?  The system call will return text data of variable length, hence part of the appeal of strstream.  I was planning on analyzing each line of the strteam using getline().

Thanks,

Guildencrantz
Avatar of migoEX
migoEX

You can easily redirect the output stream to a file, using 'freopen':
FILE *stream = freopen( "freopen.out", "w", stdout );

When you create a new process using "system", it will inherit the std* handles of your process, and then you can read this file inside your process.



>> need to redirect stdout to strstream why?
u can use files and store and retrive it

lot of ways u can achive
1. use of iles
2. stdout<< (*istringstream_ptr).rdbuf();
   stdin>>(*ostringstream_ptr).rdbuf();

3. strstream is inherited form iostream  so if stdout has the data then strstream constructor also initialises it

4. u can use getline() for istream.

kindly post u question much more cleaer so that we can help the maximum...



why ru stick on to strstream ?

anyway u have streambuffer u can decipher the contents easily....

ASKER CERTIFIED SOLUTION
Avatar of freewell
freewell

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Axter
FYI:
According to the C++ standard, strstream is deprecated, and should not be used on any new code.
It's listed in section D.7 of the deprecated section of the C++ standard.

You should use stringstream instead of strstream.

The header for stringstream is <sstream>

Axter thanks for ur info....

but section 21 also talks abt streams ....
even MSC contains <strstream>

>>even MSC contains <strstream>

Currently all C++ compliant compilers have to support strstream.

When something is depricated in the standard, that means that future versions of the standard may not support it anymore.

That means you're code is garanteed to work now, but not garanteed to work with future standards.
Also, strstream has some buggy side effects, which is one of the reasons it's been depricated in the standard.