473,396 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.escomposlinux.org/lfs-es>
Nov 14 '05 #1
3 10129
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 "interactive."

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********@yahoo.com) (cb********@worldnet.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*********@sun.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 "interactive."


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.escomposlinux.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
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: ...
3
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. ...
23
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...
6
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...
6
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...
8
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
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...
6
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...
5
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 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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,...
0
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...

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.