473,795 Members | 2,882 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

redirect system() stdout to buffer

I'd like to capture the stdout output from system("command ")
in a buffer. Although system("command >tmpnam") and then
open,read,remov e 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: j@f.com where j=john and f=forkosh )
Sep 21 '07 #1
6 8830
In article <fd**********@r eader1.panix.co m>,
JohnF <jo***@please.s ee.sig.for.addr ess.comwrote:
>I'd like to capture the stdout output from system("command ")
in a buffer. Although system("command >tmpnam") and then
open,read,remo ve 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
Sep 21 '07 #2
On Fri, 21 Sep 2007 16:22:07 +0000, Walter Roberson wrote:
In article <fd**********@r eader1.panix.co m>,
JohnF <jo***@please.s ee.sig.for.addr ess.comwrote:
>>I'd like to capture the stdout output from system("command ")
in a buffer. Although system("command >tmpnam") and then
open,read,rem ove 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

Sep 21 '07 #3
Walter Roberson <ro******@ibd.n rc-cnrc.gc.cawrote :
JohnF <jo***@please.s ee.sig.for.addr ess.comwrote:
>>I'd like to capture the stdout output from system("command ")
in a buffer. Although system("command >tmpnam") and then
open,read,rem ove 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: j@f.com where j=john and f=forkosh )
Sep 21 '07 #4
Although /off-topic/ you may find it is named _popen() on Windows.

Sep 22 '07 #5
st************* @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 )
Sep 23 '07 #6
On 23 Sep, 16:09, JohnF <jo...@please.s ee.sig.for.addr ess.comwrote:
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? :-)

Sep 23 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
5640
by: Tsai Li Ming | last post by:
Dear all, I have a problem with a redirecting stdout and stderr. I am a top level module and has no control over the imported modules that are making system calls such as os.system or popen2.* . I have tried the simplest method of capturing stdout, stderr via: saveout = sys.stdout sys.stdout = file_obj
4
10044
by: Paul Nilsson | last post by:
Hi, Does anyone know how to redirect text output when issuing a system or an execlp call? I have a GUI that insists on opening up a console whenever I give a system function call, and I would like to redirect the text output to a char buffer instead. Paul
10
21142
by: Michael Gaab | last post by:
If I redirect stdout by using freopen("afile", "w", stdout); and then I closed stdout using, fclose(stdout), essentially I am just closing "afile". I have to reestablish what stdout originally pointed to? thanks
2
4174
by: mboyda | last post by:
I have a block of code that useses outputstream to save a pdf to a client computer. It prompts the user with the save dialog, works great but the response finishes after the download completes. How do I redirect the users to a new friendly page after the download completes. The aspx page comes back with a ugly "action canceled". I prefer not to use popup windows as alot of people are blocking them. I just want them to click a download...
3
3105
by: mikem76 | last post by:
How do I automatically redirect stdout and stderr when using os.popen2 to start a long running process. If the process prints a lot of stuff to stdout it will eventually stop because it runs out of buffer space. Once I start reading the stdout file returned by os.popen2 then the process resumes. I know that I could just specify > /dev/null when starting the process but I'd like to know if there is a way to start a process using os.popen2...
6
3322
by: cmk128 | last post by:
Hi here is my c file, compile in gcc 3.X in linux: #include <stdio.h> int main() { printf("Hello\n"); if (fork() == 0) printf("world! \n"); }
5
21074
by: ws.taylor | last post by:
I am in a systems programming class, using C on a Solaris 10 development server. Part of the first programming assignment calls for the output of a command executed by a child process with execvp to be redirected to a buffer so that it can be sent through a named pipe back to the parent process. The only part I'm having problems with is capturing execvp's output to a buffer. I can't find anything about this from our book, or from searching...
3
6149
by: Alejandro | last post by:
Hi: I want to redirect stdout to a textctrl I have. From what I read in the wxpython documentation, I can use the wxLogTextCtrl class to do this. I am doing the following: class MyGui(gui.MyFrame): #gui.MyFrame generated by wxGlade def __init__(self, *args, **kwds): gui.MyFrame.__init__(self, *args, **kwds) # ... code removed ...
2
4715
by: loketing | last post by:
Is there any way to redirect the output of the system() function? I know there is for redirecting stdout, but I can't make this work with system(). I tried to store the output of a system command in a file, like so: FILE *File = NULL; freopen_s(&File, "somefile.txt", "w", stdout); system ("dir"); fclose(stdout); ...but this doesn't help. Is there any way to do this? Thank you.
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
10216
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10002
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7543
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.