Solved

stdout to strstream?

Posted on 2003-11-19
8
732 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Building cUrl in Windows v7.43.0 4 28
C++ - Convert a wString to char * 9 517
returning a dereferenced pts in C++ 10 142
how to convert c++ code to Android App 3 83
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now