473,725 Members | 2,264 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

whether is the standard input stream full buffered or line buffered after calling function setbuf()?

hi,everybody.
I calling function setbuf() to change the characteristic of standsrd
input buffer.
some fragment of the progrem is:
(DEV-C++2.9.9.2)
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char buf[10] = {0};
int i;
int j;
char c;

setbuf(stdin, buf);
c = getchar();
for(j = 0; j < 10; j++)
{
printf("%d ",buf[j]);
j++;
}
printf("\n over \n");

return 0;
}

the following questions confused me for a long time.
1) how to judge whether the standard input stream is full buffered or
line buffered .what about it after calling the function setbuf(stdin,
buf)?

2) when I enter an letter 'a', the result is
97 10 10 0 0 0 0 0 0 0
where does the second '10' come from? does it due to the OS(I use
Windows 2000 professional)?

3)how does the system flush the buffers of I/O streams? are there any
differences between flushing input buffer and output buffer?

4)If I calling setbuf(stdin, (char *)0) to set the input buffer as no
buffer, does it work for all standard input functions or just for one
function near to it, for example:

/*DEV-C++*/
#include <stdio.h>

int main(void)
{
int i,j;
char c;

setbuf(stdin, (char *)0); /* 1 */

printf("\n do you want to cal:y/n \n");
while ((c = getchar()) == 'y')
{
printf("input number:\n");
scanf("%d%d", &i, &j);
printf("i*j = %ld", i*j);
setbuf(stdin, (char *)0); /* 2*/
printf("\n do you want to cal:y/n \n");
}

return 0;
}

should I select 1 or 2, though the first doesn't work? but why?

mang many questions, I am so confused on streams and buffers.
any help will be appreciated deeply.

kernelxu

Nov 15 '05 #1
9 4009
In article <11************ *********@g47g2 000cwa.googlegr oups.com>,
<ke******@hotma il.com> wrote:
I calling function setbuf() to change the characteristic of standsrd
input buffer. #include <stdio.h>
#include <stdlib.h>
int main(void)
{
char buf[10] = {0};
int i;
int j;
char c;

setbuf(stdin, buf);
c = getchar();
for(j = 0; j < 10; j++)
{
printf("%d ",buf[j]);
j++;
}
printf("\n over \n");

return 0;
} the following questions confused me for a long time.
1) how to judge whether the standard input stream is full buffered or
line buffered .what about it after calling the function setbuf(stdin,
buf)?
In order to determine the kind of buffering that a particular
stream has, one must use system-specific measures.

2) when I enter an letter 'a', the result is
97 10 10 0 0 0 0 0 0 0
where does the second '10' come from? does it due to the OS(I use
Windows 2000 professional)?
That 10 are the decimal representation of newline characters.

Note: I do not have my copy of the C89 standard here. The online
man page I am looking at suggests that in your program, the last
8 bytes of the supplied buffer, buf[2] thru buf[9], are used for
internal purposes rather than to store characters. If that is the
case, then the second 10 might be an artifact rather than an entered byte.

3)how does the system flush the buffers of I/O streams?
The FILE* data structure includes overhead information to indicate
the portion of the buffer that is used. The flush operation passes
that data to the operating system; the details of how it does that
are operating system dependant and subject to change without notice.
are there any
differences between flushing input buffer and output buffer?
Yes. In standard C, the operation of flushing an input buffer is not
defined.

4)If I calling setbuf(stdin, (char *)0) to set the input buffer as no
buffer, does it work for all standard input functions or just for one
function near to it,
All operations on that stream, until the stream is closed or
set*buf* called again.
#include <stdio.h>
int main(void)
{
int i,j;
char c;
setbuf(stdin, (char *)0); /* 1 */
printf("\n do you want to cal:y/n \n");
while ((c = getchar()) == 'y')
{
printf("input number:\n");
scanf("%d%d", &i, &j);
printf("i*j = %ld", i*j);
setbuf(stdin, (char *)0); /* 2*/
printf("\n do you want to cal:y/n \n");
}
return 0;
} should I select 1 or 2, though the first doesn't work? but why?


You only need once.

If the first "doesn't work" then I can suggest a possible reason.
When you do the scanf(), the newline after the two numbers "conflicts"
with the %d format in order to terminate the reading of the number.
That newline is "left unread in the input stream". When you then do
the getchar(), that newline is going to be what is read, and it
isn't going to be 'y' so the while is going to terminate.

In the same vein: you generally need to enter a newline after the initial
'y' because although the input might not be "buffered" by the time it
gets to your program, it might be buffered at the shell -- setting
stdin to be unbuffered is *not* the same as undertaking system-
specific methods to turn on "raw" mode. This newline after the 'y'
is not happening to cause you problems because when you scanf() for i and j,
scanf is defined to skip -leading- whitespace -- and newline is
considered whitespace.

It is easy to fall into the habit of thinking that scanf()
starts at the beginning of the current line and "consumes" the
newline at the end, but it's just the opposite: it "consumes"
the -prior- newlines and leaves the -current- lineline in the buffer
(unless your format specifically asks for it.)
--
Ceci, ce n'est pas une idée.
Nov 15 '05 #2
thank you very much, Walter.
Walter says:
In order to determine the kind of buffering that a particular
stream has, one must use system-specific measures. so, if I assumed the standard input stream is line buffered, is it
still line buffered after calling "setbuf(std in, buf)" ? on the other
word, the difference between setbuf() and
setvbuf() is that the latter can change the buffer mode but the former
can't.

Note: I do not have my copy of the C89 standard here. The online
man page I am looking at suggests that in your program, the last
8 bytes of the supplied buffer, buf[2] thru buf[9], are used for
internal purposes rather than to store characters. If that is the
case, then the second 10 might be an artifact rather than an entered byte. I can't understand it completely, would you please give me some webpage
links such as
the man page you have found?

Yes. In standard C, the operation of flushing an input buffer is not
defined. and,if I change the standard input buffer into my own buffer, is it
defined when I flush it?
for instance:

#include <stdio.h>
int main(void)
{
int i,j;
char c;
char my_buf[256];

setbuf(stdin, my_buf);

printf("\n do you want to cal:y/n \n");
while ((c = getchar()) == 'y')
{
printf("input number:\n");
scanf("%d%d", &i, &j);
printf("i*j = %ld", i*j);
fflush(stdin);
printf("\n do you want to cal:y/n \n");
}

return 0;
}

The program works or not due to specific system. it is fine on
Windows2000+DEV-C++, but somebody tell me it fails on his system( I
don't the details, but it does fail).

In the same vein: you generally need to enter a newline after the initial
'y' because although the input might not be "buffered" by the time it
gets to your program, it might be buffered at the shell -- setting
stdin to be unbuffered is *not* the same as undertaking system-
specific methods to turn on "raw" mode. This newline after the 'y'
is not happening to cause you problems because when you scanf() for i and j,
scanf is defined to skip -leading- whitespace -- and newline is
considered whitespace.

the problem becomes more and more complex, what should I do on earth
when confronted with input problem relevant with the buffer?
is it a good idea by calling setbuf() or setvbuf()?
or, are there any other efficient methods?

thank you very much ! looking forward to your reply.

Nov 15 '05 #3
In article <11************ *********@z14g2 000cwz.googlegr oups.com>,
<ke******@hotma il.com> wrote:
Walter says:
In order to determine the kind of buffering that a particular
stream has, one must use system-specific measures.
so, if I assumed the standard input stream is line buffered,
That is not a safe assumption; input will only be line buffered if
it has been set to be line buffered. The default for input is fully
buffered.
is it
still line buffered after calling "setbuf(std in, buf)" ?
According to the [SGI IRIX] manual page I am looking at, if buf
is NULL then the I/O will be unbuffered, and otherwise it will
be line-buffered if the stream is associated with a terminal.
on the other
word, the difference between setbuf() and
setvbuf() is that the latter can change the buffer mode but the former
can't.
No, setbuf() changes the mode, but A) has no way to force line
buffering (to a non-terminal); B) has no way to force full
buffering (to a terminal); and C) assumes that the buffer
is a particular minimum size whereas the size is a parameter
for setvbuf().

. In standard C, the operation of flushing an input buffer is not
defined. and,if I change the standard input buffer into my own buffer, is it
defined when I flush it?
No. The C standard only defines flushing output buffers.
fflush(stdin) will NOT work on most systems.

This newline after the 'y'
is not happening to cause you problems because when you scanf() for i and j,
scanf is defined to skip -leading- whitespace -- and newline is
considered whitespace.

the problem becomes more and more complex, what should I do on earth
when confronted with input problem relevant with the buffer?


The default buffering works well in most situations, and
many of the rest of the situations can be dealt with by
flushing output at key locations.

Your problems are not with buffering: your problems are with
not understanding how to handle newlines.
--
This signature intentionally left... Oh, darn!
Nov 15 '05 #4
ke******@hotmai l.com wrote:
hi,everybody.
I calling function setbuf() to change the characteristic of standsrd
input buffer.
some fragment of the progrem is:
(DEV-C++2.9.9.2)
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char buf[10] = {0};
int i;
int j;
char c;

setbuf(stdin, buf);
This is wrong. The second argument to setbuf() must either
be NULL or must point to an area of at least BUFSIZ characters.
Unless your implementation' s BUFSIZ is ten or less (I've never
encountered so small a value), your program goes completely off
the rails right here.
c = getchar();
for(j = 0; j < 10; j++)
{
printf("%d ",buf[j]);
j++;
}
printf("\n over \n");

return 0;


This is wrong, too. You've asked stdin to use a buffer
that will cease to exist as soon as main() returns, but when
main() returns stdin is still "alive" and hasn't been closed.
If any shutting-down operations try to make use of the now-
vanished buffer, there's no telling what could happen. (True,
this is usually more troublesome for output streams than for
input, but you're begging for trouble.)

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 15 '05 #5
On Sun, 14 Aug 2005 08:36:08 -0400, Eric Sosman wrote:

[...]
Unless your implementation' s BUFSIZ is ten or less (I've never
encountered so small a value)


C89 requires it to be at least 256.

--
http://members.dodo.com.au/~netocrat

Nov 15 '05 #6
Walter, Eric and Netocrat
Thank you very much.
the view instantly clears up .

Nov 15 '05 #7
On Sun, 14 Aug 2005 09:35:11 +0000 (UTC), ro******@ibd.nr c-cnrc.gc.ca
(Walter Roberson) wrote:
In article <11************ *********@z14g2 000cwz.googlegr oups.com>,
<ke******@hotma il.com> wrote:
Walter says:
In order to determine the kind of buffering that a particular
stream has, one must use system-specific measures.
so, if I assumed the standard input stream is line buffered,


That is not a safe assumption; input will only be line buffered if
it has been set to be line buffered. The default for input is fully
buffered.

According to the standard, the default for stdin (and out) is fully
buffered 'if and only if the stream can be determined not to refer to
an interactive device'. But it can be hard or even impossible for an
implementation to make this determination accurately, so it's best not
to assume either way.
is it
still line buffered after calling "setbuf(std in, buf)" ?


According to the [SGI IRIX] manual page I am looking at, if buf
is NULL then the I/O will be unbuffered, and otherwise it will
be line-buffered if the stream is associated with a terminal.

NULL certainly must make it unbuffered at the C-library level, and a
valid buf (as already noted should be BUFSIZ not 10) fully buffered.
However, terminal input is line-oriented in most OS'es, including
AFAIK IRIX, so full has same effect as line. (As you correctly
explained in another thread by I believe the same OP!)
on the other
word, the difference between setbuf() and
setvbuf() is that the latter can change the buffer mode but the former
can't.


No, setbuf() changes the mode, but A) has no way to force line
buffering (to a non-terminal); B) has no way to force full
buffering (to a terminal); and C) assumes that the buffer
is a particular minimum size whereas the size is a parameter
for setvbuf().

A yes. B it does set full buffering _in C_ which should be effective
_to_ a terminal (on stdout, or stderr or other) but as above on most
systems not for input. C yes.

<snip rest>

- David.Thompson1 at worldnet.att.ne t
Nov 15 '05 #8
On 14 Aug 2005 00:48:48 -0700,
ke******@hotmai l.com <ke******@hotma il.com> wrote:

thank you very much, Walter.
Walter says:
In order to determine the kind of buffering that a particular
stream has, one must use system-specific measures.

so, if I assumed the standard input stream is line buffered, is it
still line buffered after calling "setbuf(std in, buf)" ? on the other
word, the difference between setbuf() and
setvbuf() is that the latter can change the buffer mode but the former
can't.


What is the assumed difference between stdin being line buffered or fully
buffered? I would assume that in either case the stdio functions will
get whatever is available from the OS on every read. If the OS gives
you one line at a time, then that is what you get, but if it is like
unix and windows, just gives you a unstructured byte streadm there is
no way you can force the OS to give you data one line at a time.

Villy
Nov 15 '05 #9

In article <sl************ ***@station02.o hout.pharmapart ners.nl>, Villy Kruse <ve*@station02. ohout.pharmapar tners.nl> writes:

What is the assumed difference between stdin being line buffered or fully
buffered? I would assume that in either case the stdio functions will
get whatever is available from the OS on every read.
The implementation will likely "get whatever is available", but if
stdin is line buffered it should only return it to the caller if it
encounters a line terminator (or any condition it interprets as an
error or end-of-file). And if stdin is fully buffered, it should
only return data to the caller when the buffer is full (or it gets an
error or EOF).
If the OS gives
you one line at a time, then that is what you get, but if it is like
unix and windows, just gives you a unstructured byte streadm there is
no way you can force the OS to give you data one line at a time.


There's no need to force the OS to do anything. The implementation
does the buffering. When you call fgets(), the implementation is
free to wait until doomsday to return, if it wants to wait for a line
terminator or some other condition.

--
Michael Wojcik mi************@ microfocus.com

HTML is as readable as C. You can take this either way. -- Charlie Gibbs
Nov 15 '05 #10

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

Similar topics

7
2343
by: Steven T. Hatton | last post by:
I haven't looked very closely, but from what I'm seeing, it looks like there are no buffered I/O streams in the Standard Library. There are stream buffers, but not buffered streams. I don't have an excellent definition of what a buffered stream is right off the top of my head, but it's something like a cache that can hold data from the source, or for the destination that can't be immediately processed. Say, for example, if you are...
6
15817
by: LaBird | last post by:
Dear all, I would like to ask if there is any C function that accept one keystroke as input, without printing out what the user presses and without the need to press enter as delimiter. getchar() can accept a character, but it needs user to press enter to accept the input. Thanks very much.
7
9987
by: Lee | last post by:
Hi, I'm a stream virgin and am attempting to output strings to a file. My approach is to write the string initially to a 'stringstream' and only when complete write the stringstream to the file (ofstream). The process works fine however appears to be rather slow. For example outputting about 2Mb of data takes a couple of minutes (most of the time appears to be writing to the stringstream) and as I'm creating several hundred files in...
2
1488
by: fabricemarchant | last post by:
Hi ! I'm writing a tiny interpreted language. The interpreter will first be able to load a list of source files before giving the hand to the keyboard : I use GNU readline/history library that returns the line input string that could be converted into a stream by std::istrstream. My aim is to cut the interpreter from this input system by feeding it with a unique input stream. Maybe I could use <sstream> to build this input stream. It...
22
2499
by: David Mathog | last post by:
One thing that keeps coming up in this forum is that standard C lacks many functions which are required in a workstation or server but not possible in an embedded controller. This results in a plethora of "don't ask here, ask in comp.x.y instead", for queries on functions that from the documentation available to the programmer appear to be part of the C language. I think largely because of this "least common denominator" C language...
27
2822
by: Simon Biber | last post by:
The following Example 3 is given in the 1999 C standard for the function fscanf: I have tested several implementations and none of them get the last case right. In no case does fscanf return 0 indicating failure to match "100ergs of energy" with "%f". The actual behaviour varies. Some will match '100', leaving the 'e' unread:
4
3018
by: Philipp.Weissenbacher | last post by:
Hi all! I wrote the following as a test case for a menu based programme: #include <iostream> #include <cstring> using namespace std; #define cls system("cls") const int SIZE = 10;
21
3955
by: yogicoder | last post by:
following is the code to accept a string from user, but i get segmentation fault as soon i have finished entering the string i.e. as soon as i press the 'enter' key. #include <stdio.h> #include <string.h> #include <stdlib.h> int main (void)
27
3156
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
I have a fully-portable C program (or at least I think I do). It works fine on Windows, but malfunctions on Linux. I suspect that there's something I don't know about the standard input stream that's causing the problem. Here's how I wrote the program originally: #include <stdio.h> #include <string.h>
0
8889
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9116
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
8099
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
6702
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
6011
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
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3228
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
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
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.