Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 275
  • Last Modified:

setvbuf is not working completely

Hi

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));
strcpy(a,"hllo");
fwrite(a,5,1,p1);
fwrite(a,5,1,p1);
fflush(p1);
fclose(p1);

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?

Regards

Pattha
0
pattha
Asked:
pattha
1 Solution
 
grg99Commented:
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.

0
 
_corey_Commented:
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.

corey
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now