setvbuf is not working completely

Posted on 2005-04-14
Last Modified: 2010-04-15

i am working on

SunOS au1646s 5.8 Generic_117000-01 sun4u sparc SUNW,Sun-Fire-15000

i am working on different option of setvbuf function.

FILE *p1;
char a[10],*b,c[1024];
b = (char *)malloc(10);
p1 = fopen("1","w+");
printf("\n setting buffer result = %d\n",setvbuf(p1,c,_IOFBF, 8));

Now as per my understanding, after 1st fwrite, word "hllo" is still in buffer "c" and not gone to file "1", since buffer has not been filled completely to allocated size i.e. 8 bytes.
But 2nd fwrite completes this size and after call of 2nd fwrite, i should be able to see something in file "1".But i didn't find anything there.After call of fflush, i see "hllohllo" in file.Why?

2nd question: Since i am using my own buffer namely "c", i tried to print content of array "c" after call of fwrite, but i didn't find anything there.Why?


Question by:pattha
    LVL 22

    Accepted Solution

    Most disks have a minum write size of one disk sector, often 512 bytes.  If you write less than that, the system or I/O library buffers that somewhere until 512 bytes are written or you do an explicit flush().

    As to why you don't see the data in your local buffer, maybe the I/O library is smart enough to see your buffer is smaller than it's sector buffer and skips using your buffer entirely.

    LVL 8

    Expert Comment

    You should read any manual pages or notes on the setvbuf implementation on your SunOS.  As grg99 noted it is possible that it uses its own buffer from a size constraint, but typically it only does this when you specify a NULL for the buffer location.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now