You are not logged in.

#1 2012-02-14 04:14:26

0_0
Member
From: PRC
Registered: 2011-10-31
Posts: 10

setvbuf didn't work in linux?

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

#2 2012-02-16 02:21:40

sujoy
Member
From: Finland
Registered: 2008-02-08
Posts: 95

Re: setvbuf didn't work in linux?

Try printing the buffer to test the result.

Last edited by sujoy (2012-02-16 02:23:27)

Offline

#3 2012-02-16 04:39:03

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,201

Re: setvbuf didn't work in linux?

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

#4 2012-02-16 05:54:28

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 859
Website

Re: setvbuf didn't work in linux?

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

#5 2012-02-17 02:37:09

0_0
Member
From: PRC
Registered: 2011-10-31
Posts: 10

Re: setvbuf didn't work in linux?

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

#6 2012-02-17 02:52:30

skunktrader
Member
From: Brisbane, Australia
Registered: 2010-02-14
Posts: 1,576

Re: setvbuf didn't work in linux?

Nothing to see here...please delete this post

Last edited by skunktrader (2012-02-17 02:54:53)

Offline

#7 2012-02-17 15:24:05

Blµb
Member
Registered: 2008-02-10
Posts: 224

Re: setvbuf didn't work in linux?

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

#8 2012-02-18 07:43:10

0_0
Member
From: PRC
Registered: 2011-10-31
Posts: 10

Re: setvbuf didn't work in linux?

Blµb wrote:

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. cool

Last edited by 0_0 (2012-02-18 07:43:58)


Yes,I am

Offline

#9 2012-02-19 02:06:56

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 859
Website

Re: setvbuf didn't work in linux?

0_0 wrote:

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.

The C99 Standard, 7.19.3.3, Files wrote:

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.

The C99 Standard, 7.19.5.6, The setvbuf function wrote:

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

#10 2012-02-19 02:44:17

0_0
Member
From: PRC
Registered: 2011-10-31
Posts: 10

Re: setvbuf didn't work in linux?

Oh,thank you .Could you tell me how to use that function correctly?


Yes,I am

Offline

#11 2012-02-21 08:02:09

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 859
Website

Re: setvbuf didn't work in linux?

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

#12 2012-02-23 11:50:21

0_0
Member
From: PRC
Registered: 2011-10-31
Posts: 10

Re: setvbuf didn't work in linux?

Sorry,busy these days.I use it for nothing,just learning its usage. smile


Yes,I am

Offline

Board footer

Powered by FluxBB