File Handling Problem..

Nov 14, 2012 at 7:32pm
Say i open a file using fopen() for writing and leave it open..
i keep writing to it, finally the buffer will reach its limit save the data to file(just like fflush)
right?
i want to know when will this happen, what is the limit of buffer? can i change it to my desired size like 10kb or 1mb?
Please help, i googled but no luck
Thanks
Nov 14, 2012 at 8:21pm
The default C I/O buffer size is usually the same as the preprocessor macro BUFSIZ (defined in stdio.h), although there is no requirement that it is exactly that.

In my tests, Linux and IBM use exactly BUFSIZ, but Sun doesn't. It is an implementation detail of the C library, and the only way to find out for sure is to examine the library source code, or trace the system calls made by a test program.

You can change the buffer or the condition when the flush happens by calling setbuf() or setvbuf().
Last edited on Nov 14, 2012 at 8:31pm
Nov 14, 2012 at 10:07pm
I was experimenting in this area recently, to see the effect on a filecopy program.
I think its setvbuf() which allows the buffer size to be changed.
http://www.cplusplus.com/reference/clibrary/cstdio/setbuf/
http://www.cplusplus.com/reference/clibrary/cstdio/setvbuf/
Nov 16, 2012 at 10:35am
Okay i tried using setbuf(), set the buffer size to 10 bytes but it does not save data after every 10 bytes..
i did this

opened the file for writing
wrote around 15 characters (equal 15 bytes) to it
did not close the file/did not terminate the program
data was not saved
data was not saved even after i terminated the program

please provide a code, if you have it working
Thanks :)
Nov 16, 2012 at 11:06am
Please try setvbuf(). The function allows to specify the mode and size of the buffer (in bytes).
Nov 18, 2012 at 10:52am
provide a code please :)
Nov 18, 2012 at 7:04pm
Topic archived. No new replies allowed.