Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > redirect system() stdout to buffer

Reply
Thread Tools

redirect system() stdout to buffer

 
 
JohnF
Guest
Posts: n/a
 
      09-21-2007
I'd like to capture the stdout output from system("command")
in a buffer. Although system("command >tmpnam") and then
open,read,remove tmpnam works, it's a bit more messy than
I'd like. Is there any way to redirect system()'s stdout
directly to an internal memory buffer before issuing the call?
Thanks,
--
John Forkosh ( mailto: where j=john and f=forkosh )
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      09-21-2007
In article <fd0pr1$s1v$>,
JohnF <> wrote:
>I'd like to capture the stdout output from system("command")
>in a buffer. Although system("command >tmpnam") and then
>open,read,remove tmpnam works, it's a bit more messy than
>I'd like. Is there any way to redirect system()'s stdout
>directly to an internal memory buffer before issuing the call?


Not within Standard C. Recall that *all* the C standards say
about the interpretation of the system() command is that
you can probe for the -existance- of an interpreter by passing
the NULL pointer: otherwise, what happens about the string is
completely system dependant.

If you are assuming that the system() command has a mechanism
for producing some kind of useable output, then you are making
a non-portable assumption, and so you might as well go ahead and
add in some kind of more blatant non-portability that does the
job you need. For example, if you are on a system with POSIX
extensions, you could (at no loss of portability since what you
want to do is non-portable anyhow) use popen() .
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
 
Reply With Quote
 
 
 
 
Army1987
Guest
Posts: n/a
 
      09-21-2007
On Fri, 21 Sep 2007 16:22:07 +0000, Walter Roberson wrote:

> In article <fd0pr1$s1v$>,
> JohnF <> wrote:
>>I'd like to capture the stdout output from system("command")
>>in a buffer. Although system("command >tmpnam") and then
>>open,read,remove tmpnam works, it's a bit more messy than
>>I'd like. Is there any way to redirect system()'s stdout
>>directly to an internal memory buffer before issuing the call?

>
> Not within Standard C. Recall that *all* the C standards say
> about the interpretation of the system() command is that
> you can probe for the -existance- of an interpreter by passing
> the NULL pointer: otherwise, what happens about the string is
> completely system dependant.
>
> If you are assuming that the system() command has a mechanism
> for producing some kind of useable output, then you are making
> a non-portable assumption, and so you might as well go ahead and
> add in some kind of more blatant non-portability that does the
> job you need.

There are different systems where system("foo > bar") executes
foo placing its output in bar. One might write code portable
across those but not elsewhere. "Either portable everywhere or
useful on only one architecture" is a false dilemma (look up
Wikipedia). (Anyway, I can not think of a way to do what the OP
wants on all those systems, so, if he only wants to support one
architecture, he should ask in a group about it.)
--
Army1987 (Replace "NOSPAM" with "email")
If you're sending e-mail from a Windows machine, turn off Microsoft's
stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
characters through your mail. -- Eric S. Raymond and Rick Moen

 
Reply With Quote
 
JohnF
Guest
Posts: n/a
 
      09-21-2007
Walter Roberson <> wrote:
> JohnF <> wrote:
>>I'd like to capture the stdout output from system("command")
>>in a buffer. Although system("command >tmpnam") and then
>>open,read,remove tmpnam works, it's a bit more messy than
>>I'd like. Is there any way to redirect system()'s stdout
>>directly to an internal memory buffer before issuing the call?

>
> Not within Standard C. Recall that *all* the C standards say
> about the interpretation of the system() command is that
> you can probe for the -existance- of an interpreter by passing
> the NULL pointer: otherwise, what happens about the string is
> completely system dependant.
>
> If you are assuming that the system() command has a mechanism
> for producing some kind of useable output, then you are making
> a non-portable assumption, and so you might as well go ahead and
> add in some kind of more blatant non-portability that does the
> job you need. For example, if you are on a system with POSIX
> extensions, you could (at no loss of portability since what you
> want to do is non-portable anyhow) use popen() .


Thanks, Walter, ditto Army1987.
Looks to me like popen() will work fine. I'm basically
concerned about Unix and Windows. And a little googling seems
to suggest most Windows compilers support popen(), though there
appear to be a few obscure differences in behavior that
someone or other occasionally tripped over.
Thanks, again, for bringing popen() to my attention.
--
John Forkosh ( mailto: where j=john and f=forkosh )
 
Reply With Quote
 
stephenwvickers@googlemail.com
Guest
Posts: n/a
 
      09-22-2007
Although /off-topic/ you may find it is named _popen() on Windows.

 
Reply With Quote
 
JohnF
Guest
Posts: n/a
 
      09-23-2007
wrote:
> Although /off-topic/ you may find it is named _popen() on Windows.


Terrific. Another #ifdef for Windows.
Just what I wanted.
--
John Forkosh ( mailto: where j=john and f=forkosh )
 
Reply With Quote
 
stephenwvickers@googlemail.com
Guest
Posts: n/a
 
      09-23-2007
On 23 Sep, 16:09, JohnF <jo...@please.see.sig.for.address.com> wrote:
> stephenwvick...@googlemail.com wrote:
> > Although /off-topic/ you may find it is named _popen() on Windows.

>
> Terrific. Another #ifdef for Windows.
> Just what I wanted.
> --
> John Forkosh ( mailto: j...@f.com where j=john and f=forkosh )


Come on, you wouldn't expect anything else would you?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Redirect stdout to a buffer Ecir Hana Python 6 12-10-2009 02:55 PM
beginner Q: Kernel#puts, STDOUT, $stdout relation Andreas S Ruby 3 12-09-2006 12:39 AM
Problems redirecting STDOUT (NOT sys.stdout) to a pipe. Elad Python 0 03-19-2006 01:30 PM
copy stdout fails with permission denied when stdout is redirected brian.mabry.edwards@gmail.com Perl Misc 2 12-07-2005 10:49 PM
Basic Q - Response.Redirect, all redirect to first Response.Redirect statement Sal ASP .Net Web Controls 1 05-15-2004 03:46 PM



Advertisments