K
kernelxu
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
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