Solved

stdout to strstream?

Posted on 2003-11-19
8
734 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:guildencrantz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 5

Expert Comment

by:migoEX
ID: 9785222
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.

0
 
LVL 4

Expert Comment

by:havman56
ID: 9785260


>> 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...


0
 
LVL 4

Expert Comment

by:havman56
ID: 9785267

why ru stick on to strstream ?

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

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Accepted Solution

by:
freewell earned 500 total points
ID: 9785969
The stdio of command execution is redirected to pipe where you can read in your program as a input file.

int main(int argc, char* argv[])
{
   char   psBuffer[128];
   FILE   *pPipe;
   char   psCommand[] = "dir *.c /on /p";

   if( (pPipe = _popen( psCommand, "rt" )) == NULL )
      exit( 1 );

   while( !feof( pPipe ) )
   {
      if( fgets( psBuffer, 128, pPipe ) != NULL )
         printf( psBuffer );
   }

   printf( "\nProcess returned %d\n", _pclose( pPipe ) );

   return 0;
}
0
 
LVL 30

Expert Comment

by:Axter
ID: 9786675
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>

0
 
LVL 4

Expert Comment

by:havman56
ID: 9798819
Axter thanks for ur info....

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

0
 
LVL 30

Expert Comment

by:Axter
ID: 9801077
>>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.
0
 
LVL 30

Expert Comment

by:Axter
ID: 9801083
Also, strstream has some buggy side effects, which is one of the reasons it's been depricated in the standard.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question