Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Getting C stream buffer size

Reply
Thread Tools

Getting C stream buffer size

 
 
boltar2003@boltar.world
Guest
Posts: n/a
 
      10-17-2012
Hi

Its possible to set the buffer size for stream I/O using setvbuf() but
does anyone know how to get the default buffer size?

Thanks

B2003

 
Reply With Quote
 
 
 
 
ZarkXe
Guest
Posts: n/a
 
      10-17-2012
Hi,

You can try int size = strlen(buffer);

ZarkXe.

Le 10/17/2012 11:22 AM, http://www.velocityreviews.com/forums/(E-Mail Removed) a écrit :
> Hi
>
> Its possible to set the buffer size for stream I/O using setvbuf() but
> does anyone know how to get the default buffer size?
>
> Thanks
>
> B2003
>


 
Reply With Quote
 
 
 
 
Miquel van Smoorenburg
Guest
Posts: n/a
 
      10-17-2012
In article <k5ltcf$un3$(E-Mail Removed)>, <(E-Mail Removed)> wrote:
>Its possible to set the buffer size for stream I/O using setvbuf() but
>does anyone know how to get the default buffer size?


Depends on your C library. Often it will be BUFSIZ, but a library
might choose to use a different size based on filesystem hints.

If you're using glibc you can use __fbufsize (FILE *stream) to get
the buffer size for a stream, see also:
http://www.gnu.org/software/libc/man...Buffering.html

Mike.
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      10-17-2012
On 10/17/2012 06:55 AM, ZarkXe wrote:
> Hi,
>
> You can try int size = strlen(buffer);


How do you know what name the buffer has? It's certainly not buffer. It
might be file->__buffer.

Do you know what that code will do if there are no null characters in
the buffer? Do you have any particular reason to believe that there are
any null characters in that buffer? In particular, do you have any
particular reason to believe that there's exactly one null character in
the buffer, and that it's at the end of the buffer?

> Le 10/17/2012 11:22 AM, (E-Mail Removed) a écrit :
>> Hi
>>
>> Its possible to set the buffer size for stream I/O using setvbuf() but
>> does anyone know how to get the default buffer size?


BUFSIZE, #defined in <stdio.h> gives you "the size of the buffer used by
the setbuf function" (7.21.1p3) There's a good chance that this is also
the default buffer size for a file where you've never called setbuf() or
setvbuf(), though that is not guaranteed.

If you can afford to write unportable code, there may be
implementation-specific ways of doing this. If stdio.h defines FILE as a
struct type, you'll often find that it contains a member which gives the
size of the buffer, or one that points at the end of the buffer. I
wouldn't recommend this approach, however.

Why do you need to know?
--
James Kuyper
 
Reply With Quote
 
Ike Naar
Guest
Posts: n/a
 
      10-17-2012
On 2012-10-17, James Kuyper <(E-Mail Removed)> wrote:
> BUFSIZE, #defined in <stdio.h> gives you "the size of the buffer used by
> the setbuf function" (7.21.1p3) There's a good chance that this is also
> the default buffer size for a file where you've never called setbuf() or
> setvbuf(), though that is not guaranteed.


Nit: the name of the macro is BUFSIZ (without the 'E').
 
Reply With Quote
 
boltar2003@boltar.world
Guest
Posts: n/a
 
      10-17-2012
On 17 Oct 2012 11:01:58 GMT
"Miquel van Smoorenburg" <(E-Mail Removed)> wrote:
>In article <k5ltcf$un3$(E-Mail Removed)>, <(E-Mail Removed)> wrote:
>>Its possible to set the buffer size for stream I/O using setvbuf() but
>>does anyone know how to get the default buffer size?

>
>Depends on your C library. Often it will be BUFSIZ, but a library
>might choose to use a different size based on filesystem hints.
>
>If you're using glibc you can use __fbufsize (FILE *stream) to get
>the buffer size for a stream, see also:
>http://www.gnu.org/software/libc/man...Buffering.html


Thanks.

B2003


 
Reply With Quote
 
boltar2003@boltar.world
Guest
Posts: n/a
 
      10-17-2012
On Wed, 17 Oct 2012 08:06:12 -0400
James Kuyper <(E-Mail Removed)> wrote:
>Why do you need to know?


So I know if I need to bother setting it to an alternative size. And
curiosity.

B2003

 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      10-17-2012
On 10/17/2012 08:39 AM, (E-Mail Removed) wrote:
> On Wed, 17 Oct 2012 08:06:12 -0400
> James Kuyper <(E-Mail Removed)> wrote:
>> Why do you need to know?

>
> So I know if I need to bother setting it to an alternative size. And
> curiosity.


Whether you need an alternative size, and what that size should be, is
something that you can't determine in an implementation-independent way.
You have to do profiling, experimenting with different sizes to find the
best one; it generally won't be the same as the best size on a different
implementation. Since that's already inherently unportable, I'd
recommend using the non-portable methods I mentioned (but refused to
recommend) in my previous message. Find out how FILE is defined in
<stdio.h>, and make some reasonable guesses as to what the field names
mean. Also, check for an implementation-specific special function, such
a the __fbufsize() function Miquel mentioned.
--
James Kuyper
 
Reply With Quote
 
boltar2003@boltar.world
Guest
Posts: n/a
 
      10-18-2012
On Thu, 18 Oct 2012 01:48:16 -0400
"James K. Lowden" <(E-Mail Removed)> wrote:
>On Wed, 17 Oct 2012 08:56:15 -0400
>James Kuyper <(E-Mail Removed)> wrote:
>
>> On 10/17/2012 08:39 AM, (E-Mail Removed) wrote:
>> > On Wed, 17 Oct 2012 08:06:12 -0400
>> > James Kuyper <(E-Mail Removed)> wrote:
>> >> Why do you need to know?
>> >
>> > So I know if I need to bother setting it to an alternative size. And
>> > curiosity.

>>
>> I'd recommend using the non-portable methods I mentioned
>> (but refused to recommend) in my previous message.

>
>Huh. I didn't see that coming. If I know what size I need, and don't
>know what size I've got, why not set it to what I need and be done with
>it?


Because you might end up making the buffer smaller and hence the I/O less
efficient if you don't check the default size first?

B2003

 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      10-18-2012
On 10/18/2012 01:48 AM, James K. Lowden wrote:
> On Wed, 17 Oct 2012 08:56:15 -0400
> James Kuyper <(E-Mail Removed)> wrote:
>
>> On 10/17/2012 08:39 AM, (E-Mail Removed) wrote:
>>> On Wed, 17 Oct 2012 08:06:12 -0400
>>> James Kuyper <(E-Mail Removed)> wrote:
>>>> Why do you need to know?
>>>
>>> So I know if I need to bother setting it to an alternative size. And
>>> curiosity.

>>
>> I'd recommend using the non-portable methods I mentioned
>> (but refused to recommend) in my previous message.

>
> Huh. I didn't see that coming. If I know what size I need, and don't
> know what size I've got, why not set it to what I need and be done with
> it?


That's also an alternative. But if you actually know (as opposed to
incorrectly thinking that you know) the size you need, that knowledge is
system-specific, and will not apply when you port to a different system.
--
James Kuyper
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
buffer creates only read-only buffer? Neal Becker Python 0 01-08-2009 01:58 AM
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net 2 07-29-2008 09:50 AM
convert M bit buffer to N bit buffer runcyclexcski@yahoo.com C++ 2 03-26-2007 09:43 AM
How to know the buffer size and increase buffer size in c++ Raja C++ 12 06-21-2004 06:21 PM
Buffer size & Prebuffering size? Skyhorse C++ 4 04-16-2004 12:35 PM



Advertisments