• C

setvbuf is not working completely


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?


Who is Participating?
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.

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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.