setvbuf, fwrite & fread 
Author Message
 setvbuf, fwrite & fread

Hi,
 I working on an homework that requires that we use fwrite and fread.
However while doing my homework I stumbled upon th setvbuf function in
my textbook. I've used the function in my code and I've compiled and run
my code which work, but the problem is I really don't understand what
the fuction does. Can someone kindly explain what the fuction does to me
and why it is needed. I hate cutting and pasting without understanding
what I'm doing. Thank you in advance.


Sat, 29 Jan 2005 08:21:10 GMT  
 setvbuf, fwrite & fread
On Tue, 13 Aug 2002 00:21:10 GMT, in comp.lang.c , Noah ClayPole

Quote:

>Hi,
> I working on an homework that requires that we use fwrite and fread.
>However while doing my homework I stumbled upon th setvbuf function in
>my textbook. I've used the function in my code and I've compiled and run
>my code which work, but the problem is I really don't understand what
>the fuction does. Can someone kindly explain what the fuction does to me
>and why it is needed. I hate cutting and pasting without understanding
>what I'm doing. Thank you in advance.

setbuf and setvbuf control how the stream is buffered. By default,
most implementations will buffer output on many streams to improve
write efficiency. By using these functions you can override that
behaviour.

From the C99 standard:
The setvbuf function may be used only after the stream pointed to by
stream has been associated with an open file and before any other
operation (other than an unsuccessful call to setvbuf) is performed on
the stream. The argument mode determines how stream will be buffered,
as follows: _IOFBF causes input/output to be fully buffered; _IOLBF
causes input/output to be line buffered; _IONBF causes input/output to
be unbuffered. If buf is not a null pointer, the array it points to
may be used instead of a buffer allocated by the setvbuf function230)
and the argument size specifies the size of the array; otherwise, size
may determine the size of a buffer allocated by the setvbuf function.
The contents of the array at any time are indeterminate.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>



Sat, 29 Jan 2005 08:34:09 GMT  
 setvbuf, fwrite & fread

Quote:
> Hi,
>  I working on an homework that requires that we use fwrite and fread.
> However while doing my homework I stumbled upon th setvbuf function in
> my textbook. I've used the function in my code and I've compiled and run
> my code which work, but the problem is I really don't understand what
> the fuction does. Can someone kindly explain what the fuction does to me
> and why it is needed. I hate cutting and pasting without understanding
> what I'm doing. Thank you in advance.

Whenever you write to or read from a stream that has been
fopened (btw printf, scanf, getchar ... work on streams that are
fopened automagically) this happens in some buffered mode. This
means, the I/O-library functions don't pass the data you send
them directly to the system but hold it back until there is
"enough" data to write and on reading there is not only read the
amount of data requested but probably a full line of input or
say 8 kilobytes (if your system thinks that is an appropriate
value) if there is that much data available. You can use the
setbuf and setvbuf functions to enlarge or shrink the buffer
used or make the buffer a special location in your program or
change the mode in which the stream is buffered (unbuffered,
line-buffered or fully-buffered).

Buffering is not the easiest thing to fully understand in the
beginning, so don't worry to much about it at the moment.
You will come accross this in certain occasions and get to know
it.

Btw, if the requirements where only for fread() and fwrite() to
be used in your assignment I don't see a need for setbuf or
setvbuf at all. Actually I only used these functions in a useful
program for one time. In my personal logging library I set the
buffer mode for a non-interactive stream to line-buffered so
lines go directly to the log-file instead of in a block of some
kBytes (which is how non-interactive streams are usually
buffered). That is useful for debugging (to me at least).

--

"LISP  is worth learning for  the profound enlightenment  experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days."   -- Eric S. Raymond



Sat, 29 Jan 2005 14:32:33 GMT  
 setvbuf, fwrite & fread
Groovy hepcat Noah ClayPole was jivin' on Tue, 13 Aug 2002 00:21:10
GMT in comp.lang.c.
setvbuf, fwrite & fread's a cool scene! Dig it!

Quote:
> I working on an homework that requires that we use fwrite and fread.
>However while doing my homework I stumbled upon th setvbuf function in
>my textbook. I've used the function in my code and I've compiled and run
>my code which work, but the problem is I really don't understand what
>the fuction does. Can someone kindly explain what the fuction does to me
>and why it is needed. I hate cutting and pasting without understanding
>what I'm doing. Thank you in advance.

  If you have no idea what it does, why are you using it? It sounds
like you don't need it in your program. Using a function just because
you found it in a text book is rather a strange thing to do. You
should use it only because your program needs it (or your instructor
tells you to use it). If your program doesn't need it (and your
instructor hasn't set this as a requirement), don't use it.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?



Tue, 01 Feb 2005 11:49:11 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. fread() w/wo setvbuf() & buffering question

2. fwrite() & fread() question

3. FREAD & FWRITE

4. fwrite,fread,File & Structures

5. fwrite & fread

6. fread, fwrite probs

7. Please Help (fread, fwrite)

8. fwrite(), fread() portability.

9. problem with fwrite / fread

10. fopen() fseek() fread() fwrite()

11. Problem with struct/array inside a struct using fread/fwrite

12. Strange byte order using fread() and fwrite()

 

 
Powered by phpBB® Forum Software