Getting C stream buffer size

B

boltar2003

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
 
Z

ZarkXe

Hi,

You can try int size = strlen(buffer);

ZarkXe.

Le 10/17/2012 11:22 AM, (e-mail address removed) a écrit :
 
M

Miquel van Smoorenburg

J

James Kuyper

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 address removed) a écrit :

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?
 
I

Ike Naar

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').
 
J

James Kuyper

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.
 
B

boltar2003

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
 
J

James Kuyper

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top