You are not logged in.
Pages: 1
Hi,all of you!
I wrote some codes to test setvbuf function in linux.Here are the codes:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
char buf[10];
int main()
{
if((setvbuf(stdout,buf,_IOFBF,10)) != 0) {
fprintf(stderr,"setvbuf() failed: %s\n",strerror(errno));
exit(1);
}
printf("123456789");
_exit(0);
}
As you can see,it should not output anything on screen.But,unfortunately,it outputs "123456789".I test the codes with archlinux,ubuntu,opensuse,and it's all the same.As a matter of fact,it WORKED in freebsd.I just wonder if there is something wrong with my codes or the function setvbuf in linux,it confused me for a long time.
I will appreciate any comments/suggestions,and thanks in advance.
Yes,I am
Offline
Try printing the buffer to test the result.
Last edited by sujoy (2012-02-16 02:23:27)
Offline
Does this passage from man setvbuf have any relevance?
You must make sure that the space that buf points to still exists by the time stream is closed, which also happens at program termination. For example, the follow‐
ing is invalid:
#include <stdio.h>
int
main(void)
{
char buf[BUFSIZ];
setbuf(stdin, buf);
printf("Hello, world!\n");
return 0;
}
I'm not certain myself; It is something to play with.
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way
Offline
Simply put, the implementation is free to flush the stream whenever it wants. On my box, printf("1"); doesn't print anything, but printf("12") does. See this:
http://www.gnu.org/software/libc/manual … g-Concepts
Characters written to or read from an unbuffered stream are transmitted individually to or from the file as soon as possible.
Characters written to a line buffered stream are transmitted to the file in blocks when a newline character is encountered.
Characters written to or read from a fully buffered stream are transmitted to or from the file in blocks of arbitrary size.
Offline
Thanks for all responses.
Hi,ewaller.I've already noticed that passage yet,and i allocated buf statically.See my codes above.
Simply put, the implementation is free to flush the stream whenever it wants.
Characters written to or read from a fully buffered stream are transmitted to or from the file in blocks of arbitrary size.
Well,tavianator.I think the arbitrary size means the size of buffer allocated,that is,the buffer's size is arbitrary.
I don't think the implementation is free to flush the stream whenever it wants,if it's so,parameter size is no need in servbuf.
Last edited by 0_0 (2012-02-17 02:39:07)
Yes,I am
Offline
Nothing to see here...please delete this post
Last edited by skunktrader (2012-02-17 02:54:53)
Offline
Maybe a buffer size of 16 isn't considered useful... For me, the text stops being printed at a buffer size of >= 128.
You know you're paranoid when you start thinking random letters while typing a password.
A good post about vim
Python has no multithreading.
Offline
Maybe a buffer size of 16 isn't considered useful... For me, the text stops being printed at a buffer size of >= 128.
Thank you for testing the codes.It seems you are right,if the buffer have a size of >= 128,setvbuf works normally.That's instreasting.
Last edited by 0_0 (2012-02-18 07:43:58)
Yes,I am
Offline
Well,tavianator.I think the arbitrary size means the size of buffer allocated,that is,the buffer's size is arbitrary.
I don't think the implementation is free to flush the stream whenever it wants,if it's so,parameter size is no need in servbuf.
When a stream is fully buffered, characters are intended to be transmitted to or from the host environment as a block when a buffer is filled. ... Support for these characteristics is implementation-defined, and may be affected via the setbuf and setvbuf functions.
If buf is not a null pointer, the array it points to may be used instead of a buffer allocated by the setvbuf function, and the argument size specifies the size of the array
Note the use of may in two key places. The C standard does not enforce any particular behaviour here.
Offline
Oh,thank you .Could you tell me how to use that function correctly?
Yes,I am
Offline
Well, what are you trying to use it for? If you're trying to redirect output from stdout to somewhere else, use freopen() or dup2().
Offline
Sorry,busy these days.I use it for nothing,just learning its usage.
Yes,I am
Offline
Pages: 1