Solved

stdout to strstream?

Posted on 2003-11-19
8
731 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

757 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

20 Experts available now in Live!

Get 1:1 Help Now