Solved

stdout to strstream?

Posted on 2003-11-19
8
736 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
Independent Software Vendors: 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

636 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