473,772 Members | 2,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

disable stdout buffering ?

Hi.

I have the following problem:
Using popen() to execute a program and read its stdout works usually fine.
Now I try to do this with a program called xsupplicant (maybe one knows),
but I dont get the output of it while it is running.

This is probably a problem of stdout being buffered, because if I use
fflush() after a printf() in the xsupplicant then I can read the output.

My question is now: is it possible to tell stdout not to buffer anything,
but immediately print it out.

I dont want to change the xsupplicant source code.

Hope anyone understood what I am trying to say.
thanks.
Nov 15 '05 #1
7 15607
>
My question is now: is it possible to tell stdout not to buffer anything,
but immediately print it out.

Yes. Please see setvbuf and other related functions.

Regards,
Madhav.

Nov 15 '05 #2
Thanks for your reply.

On Tue, 04 Oct 2005 05:50:26 -0700, Madhav wrote:

My question is now: is it possible to tell stdout not to buffer anything,
but immediately print it out.

Yes. Please see setvbuf and other related functions.

I tried setvbuf and setbuf:

....
FILE* fp = popen("...","r" );
setvbuf(fp,NULL ,_IONBF,0);
....

But unfortunately it had no effect ...

Nov 15 '05 #3
Mathias Herrmann <herrmann@'remo ve_me'sit.fraun hofer.de> wrote:
On Tue, 04 Oct 2005 05:50:26 -0700, Madhav wrote:
My question is now: is it possible to tell stdout not to buffer anything,
but immediately print it out.

Yes. Please see setvbuf and other related functions.


I tried setvbuf and setbuf:

...
FILE* fp = popen("...","r" );
setvbuf(fp,NULL ,_IONBF,0);


Ah. You are dealing with pipes, not with normal streams. Pipes are
different; they may or may not respond to functions that work on files.
Pipes are also not part of ISO C, and different implementations of pipes
may behave in different ways. For a reliable answer, ask in a newsgroup
for whatever library defines your pipes - probably POSIX, in which case,
ask in comp.unix.progr ammer.

Richard
Nov 15 '05 #4


Mathias Herrmann wrote On 10/04/05 09:22,:
Thanks for your reply.

On Tue, 04 Oct 2005 05:50:26 -0700, Madhav wrote:

My question is now: is it possible to tell stdout not to buffer anything,
but immediately print it out.


Yes. Please see setvbuf and other related functions.


I tried setvbuf and setbuf:

...
FILE* fp = popen("...","r" );
setvbuf(fp,NULL ,_IONBF,0);
...

But unfortunately it had no effect ...


... because it was done on "the wrong end" of the
connection. You need to disable the buffering that
the sender is doing, so you need to get the sender to
call setvbuf() before doing anything else with its
stdout stream.

Unfortunately, that goes against your goal of not
wanting to make any changes to the other program's code.
However, the change is a very small one: just a one-liner
at or near the start of main() will do it.

Unfortunately (again), you're still at the mercy of
the operating system. Telling the C library not to buffer
an output stream causes fprintf() and the like to deliver
characters directly to the O/S without undue delay, but
doesn't govern what the O/S then chooses to do with them.
You'll need to read your O/S' documentation on pipes (which
aren't part of C, by the way).

<off-topic>

In my experience, many "interactiv e" programs fare
rather poorly with pipes; the I/O model really isn't quite
what's wanted. If your system provides pseudo-terminals
(/dev/pty), they might be more appropriate. For further
help along these lines -- or with pipes, for that matter --
try comp.unix.progr ammer.

</off-topic>

--
Er*********@sun .com

Nov 15 '05 #5
> ... because it was done on "the wrong end" of the
connection. You need to disable the buffering that
the sender is doing, so you need to get the sender to
call setvbuf() before doing anything else with its
stdout stream.

Unfortunately, that goes against your goal of not
wanting to make any changes to the other program's code.
However, the change is a very small one: just a one-liner
at or near the start of main() will do it.

Unfortunately (again), you're still at the mercy of
the operating system. Telling the C library not to buffer
an output stream causes fprintf() and the like to deliver
characters directly to the O/S without undue delay, but
doesn't govern what the O/S then chooses to do with them.
You'll need to read your O/S' documentation on pipes (which
aren't part of C, by the way).

<off-topic>

In my experience, many "interactiv e" programs fare
rather poorly with pipes; the I/O model really isn't quite
what's wanted. If your system provides pseudo-terminals
(/dev/pty), they might be more appropriate. For further
help along these lines -- or with pipes, for that matter --
try comp.unix.progr ammer.

</off-topic>

Thanks a lot.
Im going to try the use of a pty.
Nov 15 '05 #6
Mathias Herrmann wrote:

Hi.

I have the following problem:
Using popen() to execute a program and read its stdout works usually fine.
Now I try to do this with a program called xsupplicant (maybe one knows),
but I dont get the output of it while it is running.

This is probably a problem of stdout being buffered, because if I use
fflush() after a printf() in the xsupplicant then I can read the output.

My question is now: is it possible to tell stdout not to buffer anything,
but immediately print it out.

I dont want to change the xsupplicant source code.


You can't "unbuffer" xsupplicant's stdout without changing xsupplicant's
source code, unless this ability is already built in.

You could probably try something like this at the start of main():

if ( !isatty(stdout) )
setbuf(stdout,N ULL);

The specifics are probably OT to clc, so you might need to ask in a
group where this would be on-topic. Perhaps comp.unix.progr ammer?

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>
Nov 15 '05 #7

In article <pan.2005.10.04 .12.11.37.99398 6@'remove_me'si t.fraunhofer.de >, Mathias Herrmann <herrmann@'remo ve_me'sit.fraun hofer.de> writes:

My question is now: is it possible to tell stdout not to buffer anything,
Yes:

#include <stdio.h>

int main(void)
{
int ret;
ret = setvbuf(stdout, NULL, _IONBF, 0);
if (ret == 0)
puts("stdout is now unbuffered");
return 0;
}
but immediately print it out.


No. While you can disable the C library's buffering for stdout
(if setvbuf succeeds), you don't have any control over when the
underlying system processes the output generated by the C library.

However, I believe you've asked the wrong question, since you
appear to want to change the stdout buffering for another process,
without changing that program's source code. That you cannot do,
in standard C. (Note that popen is not part of the standard C
library.)

I suggest you take this question to a newsgroup that deals with
popen, such as comp.unix.progr ammer, or one specific to your
operating system. (OT: Unless I am gravely mistaken, there's no
POSIX/SUS mechanism to force another process to disable stdio
buffering, either. Using a pseudo-tty rather than a pipe should
get you line buffering rather than full buffering, though, which
might suffice.)
--
Michael Wojcik mi************@ microfocus.com

You brung in them two expert birdwatchers ... sayin' it was to keep us from
makin' dern fools of ourselfs ... whereas it's the inherent right of all to
make dern fools of theirselfs ... it ain't a right held by you official types
alone. -- Walt Kelly
Nov 15 '05 #8

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

Similar topics

2
2349
by: Hans Deragon | last post by:
Greetings. I am performing: commands.getstatusoutput("rsync <params>"); rsync takes a long time and prints out a steady stream of lines showing which file it is currently working on.
2
3853
by: Graham Ashton | last post by:
Hi. I'm having trouble flushing sys.stdout. I've written a small example to illustrate my problem (see below). In short, I expect it to ping "received hello", sleep for 5 seconds and then print "received world". Instead I get nothing for 5 seconds and then both statements pop out at once. As you'll no doubt gather from the example, the problem I'm really trying to solve is sending message back from one process to another via a pipe....
6
3583
by: Avi Berkovich | last post by:
Hello, I was unable to use popen2.popen4 to grab python.exe's (2.3) output, for starts, it doesn't show the version information at the beginning and won't return anything when writing to the stdin pipe, it seems that if I give it some error nous expression, the pipe would return the exception data, though nothing else comes through. A friend of mine also tried this using win32api on delphi, and got the same result.
4
6106
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my application (VB.NET) will start a process, redirect its stdout and capture that process' output, displaying it in a window. I've written a component for this, and a test application for the component. It allows me to specify a command to execute,...
1
2453
by: bine | last post by:
Sorry, I found a lot of old stuff, even kind of flamewars about good+bad style whatsoever.. I simply want any hint that works! I try to migrate older stuff from NT and newer stuff from Linux/AIX to run on Windows2003 as well. I use the Open Watcom C32 Optimizing Compiler Version 1.3 Portions Copyright (c) 1984-2002 Sybase, Inc. All Rights Reserved.
5
2209
by: Luigi | last post by:
Hi to all! I'd like to execute an external program capturing the stdout/stderr messages at "real-time". I mean that I don't want to wait for the end of the process. If I write a code like this: import os import sys class Runner:
2
2188
by: Massi | last post by:
Hi everyone! I'm writing a python script which uses a C-written dll. I call the functions in the dll using ctypes, but I don't know how to catch the output of the "printf" which the C functions use. In fact I don't even know if it is possible! I've heard something about PIPE and popen...is this what I need? How can I use them? It is very important for me that I could take the output in real-time. Thanks for the help! Massi
7
384
by: MisterPete | last post by:
How can I inherit from file but stil create an instance that writes to stdout? ----------- I'm writing a file-like object that has verbosity options (among some other things). I know I could just set self.file to a file object rather than inheriting from file. I started with something like this (simplified for clarity): class Output(object): def __init__(self, file=sys.stdout, verbosity=1):
37
5075
by: Vince C. | last post by:
Hi all. I've installed Bloodshed Dev-C++ on a Windows 2000 SP4 machine. I'm using MinGW 3.4.2. I'd like to temporarily disable standard functions to write to stderr, i.e. for instance redirect stderr to a temporary file (or /dev/null but is there an equivalent under Windows? Is it "nul:") and then to *restore* the default stderr so that standard library functions that write to stderr produce output again.
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10103
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...
0
9911
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...
0
8934
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
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
6713
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
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.