473,651 Members | 2,645 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Will reading from stdin() flush output to stdout()?

Hi:

I need some clarification about a code like this:

printf("%s", "a string with NO trailing newline");
scanf("%d", &i);

Regarding if a fflush() of the standard output is needed or not. I think
the relevant portions of the standard are:

[7.19.3.3] When a stream is unbuffered, characters are intended to
appear from the source or at the destination as soon as possible. [...]
When a stream is line buffered, characters are intended to be
transmitted to or from the host environment as a block when a new-line
character is encountered. Furthermore, characters are intended to be
transmitted as a block to the host environment when a buffer is filled,
when input is requested on an unbuffered stream, or when input is
requested on a line buffered stream that requires the transmission of
characters from the host environment. Support for these characteristics
is implementation-defined, and may be affected via the setbuf and
setvbuf functions.

[7.19.3.7] [...] As initially opened, the standard error stream is not
fully buffered; the standard input and standard output streams are fully
buffered if and only if the stream can be determined not to refer to an
interactive device.
We have been discussing about it, and there are two opinions:

- The fflush(stdout) after printf() is not necessary, since the
standard guarantees that the string is transmitted to the
environment when the input is requested.

- The fflush(stdout) after printf() is desirable, since the
implicit fflush() is not guaranteed when input is requested. It is
not guaranteed because the standard specify that "support for
these characteristics is implementation-defined".

Could you, please, tell me which (if any) is the correct reasoning?

Perhaps it is a matter of language. English is not my mother tongue, so
I may have misinterpreted the standard.

Thanks in advance,
--
Fernando Arbeiza <URL: mailto:ar****** @ono.com>
Crea tu propio Linux: <URL: http://www.escomposlin ux.org/lfs-es>
Nov 14 '05 #1
3 10144
Fernando Arbeiza wrote:

Hi:

I need some clarification about a code like this:

printf("%s", "a string with NO trailing newline");
scanf("%d", &i);

Regarding if a fflush() of the standard output is needed or not. I think
the relevant portions of the standard are:

[7.19.3.3] When a stream is unbuffered, characters are intended to
appear from the source or at the destination as soon as possible. [...]
When a stream is line buffered, characters are intended to be
transmitted to or from the host environment as a block when a new-line
character is encountered. Furthermore, characters are intended to be
transmitted as a block to the host environment when a buffer is filled,
when input is requested on an unbuffered stream, or when input is
requested on a line buffered stream that requires the transmission of
characters from the host environment. Support for these characteristics
is implementation-defined, and may be affected via the setbuf and
setvbuf functions.

[7.19.3.7] [...] As initially opened, the standard error stream is not
fully buffered; the standard input and standard output streams are fully
buffered if and only if the stream can be determined not to refer to an
interactive device.
We have been discussing about it, and there are two opinions:

- The fflush(stdout) after printf() is not necessary, since the
standard guarantees that the string is transmitted to the
environment when the input is requested.
No; the transmission is "intended" only if the input is from
(1) an unbuffered stream or (2) a line buffered stream with a
special characteristic.

Concerning (1), you know that stdin is not fully buffered if
it communicates with an interactive device. But you don't know
whether it is unbuffered or line buffered -- so you don't know
whether the "unbuffered stream" precondition is true or false.

As for (2), the special characteristic is that input operations
from the stream require the host to transmit some characters -- a
"go ahead" sequence or something of the sort. Again, you have no
way of knowing whether this is the case, so you cannot tell whether
the precondition is true. (Not portably, at any rate.)
- The fflush(stdout) after printf() is desirable, since the
implicit fflush() is not guaranteed when input is requested. It is
not guaranteed because the standard specify that "support for
these characteristics is implementation-defined".


.... and that is yet another reason you cannot rely on the automatic
fflush(stdout). Also, read section 5.1.2.3 paragraph 6:

What constitutes an interactive device is
implementation-defined.

.... which means that even if your stdin stream reads from a joystick
or a data glove, the stream could be fully buffered anyhow if the
implementation doesn't consider such devices "interactiv e."

Call fflush() explicitly when you need the output to appear
"now." Of course, even fflush() cannot guarantee that the
output will appear "soon." For example, consider a stdout stream
that transmits across a satellite link with a significant delay.

--
Er*********@sun .com
Nov 14 '05 #2
Fernando Arbeiza wrote:
.... snip ...
- The fflush(stdout) after printf() is desirable, since the
implicit fflush() is not guaranteed when input is requested.
It is not guaranteed because the standard specify that
"support for these characteristics is implementation-defined".

Could you, please, tell me which (if any) is the correct reasoning?


The one above, which I didn't snip.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #3
On Wed, 04 Feb 2004 18:28:08 -0500, Eric Sosman <Er*********@su n.com> wrote:
Fernando Arbeiza wrote:
- The fflush(stdout) after printf() is desirable, since the
implicit fflush() is not guaranteed when input is requested. It is
not guaranteed because the standard specify that "support for
these characteristics is implementation-defined".


... and that is yet another reason you cannot rely on the automatic
fflush(stdout). Also, read section 5.1.2.3 paragraph 6:

What constitutes an interactive device is
implementation-defined.

... which means that even if your stdin stream reads from a joystick
or a data glove, the stream could be fully buffered anyhow if the
implementation doesn't consider such devices "interactiv e."


I realized that but I took for granted that most implementations would
consider the console an interactive device (I know I should not have
done it; please, don't be cruel ;-).

Thank you (and Mr. Falconer) for your replys.

Regards,
--
Fernando Arbeiza <URL: mailto:ar****** @ono.com>
Crea tu propio Linux: <URL: http://www.escomposlin ux.org/lfs-es>
Nov 14 '05 #4

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

Similar topics

0
5134
by: rtm | last post by:
I am interested in running a process with a timeout. Also I'm interested in analyzing the output of this process. Under Unix, the solution is described clearly in the Perl Cookbook "16.10: Communicating between related processes" and 16.24 "Timing out an Operation". Enclosed below is an example showing what I want to do under Unix. I need to do this under Windows XP. As others have pointed out "alarm" works under 5.8+ and fork...
3
2574
by: Mike Tammerman | last post by:
Hi, I want create a subprocess using Popen and pipe some input to it. Although everything works perfectly while executing python in, it doesn't work if I try with executables made by py2exe. I think, stdin is invalidated if the program becomes an executable. Because I get a "Bad file descriptor" exception in subprogram.py. I will be more than apreciated, if any suggestions occur.
23
7724
by: herrcho | last post by:
What's the difference between STDIN and Keyboard buffer ? when i get char through scanf, i type in some characters and press enter, then, where do the characters go ? to STDIN or Keyboard buffer ? are they same ? thanks ^^
6
2475
by: Charlie Zender | last post by:
Hi, I have a program which takes the output filename argument from stdin. Once the program knows the output filename, it tries to open it. If the output file exists, the program asks the user to confirm whether he really wants to overwrite the existing output file. The problem is that the second read from stdin, to obtain the user response whether to overwrite the existing output file, never waits for the user's response. It's as if a...
6
2772
by: Christian Convey | last post by:
Hello, I've got a program that (ideally) perpetually monitors sys.stdin for lines of text. As soon as a line comes in, my program takes some action. The problem is, it seems like a very large amount of data must accumulate on sys.stdin before even my first invocation of readline() returns. This delay prevents my program from being responsive in the way it must be.
8
3809
by: Christoph Haas | last post by:
Hi... I encountered a problem that - according to my google search - other people have found, too. Example code: import sys for line in sys.stdin: print line Running this code in Python 2.3 or 2.4 has the problem that I need to
2
4033
by: kimonp | last post by:
I am running on windows XP with a fresh install of wamp5 (1.7.2) and mediawiki. I am trying to call a perl function from within php using proc_open. The perl script is being executed and returns a successful exit value, and I can read the results it prints to STDOUT in PHP, but the perl script does not see STDIN from PHP. Since I want to use the perl script as a filter and I don't want to create an intermediary file, this is problematic....
6
17543
by: gregpinero | last post by:
Let's say I have this Python file called loop.py: import sys print 'hi' sys.stdout.flush() while 1: pass And I want to call it from another Python process and read the value 'hi'. How would I do it?
5
705
by: Luis Zarrabeitia | last post by:
I have a problem with this piece of code: ==== import sys for line in sys.stdin: print "You said!", line ==== Namely, it seems that the stdin buffers the input, so there is no reply until a huge amount of text has bin written. The iterator returned by xreadlines
0
8695
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
8460
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
8576
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
7296
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...
0
5609
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
4143
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
4281
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2696
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
1585
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.