User Tools

Site Tools


c:stdio.h:setvbuf

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

c:stdio.h:setvbuf [2013/01/22 22:02] (current)
Line 1: Line 1:
 +{{keywords>wiki library source code example reference}}
 +====== setvbuf ======
 +<code c>
 +    int setvbuf(FILE *stream, char *buffer, int mode, size_t size); 
 +</code>
 +Controls buffering for stream stream. mode is _IOFBF for full buffering, _IOLBF for line buffering, _IONBF for no buffering. Non-null buf specifies buffer of size size to be used; otherwise, a buffer is allocated. Returns non-zero on error. Call must be before any other operation on stream.
 +<code c>
 +    _IOLBF   File will buffered line by line
 +    _IONBF   Input/Output will not be buffered
 +    _IOFBF   Input/Output will fully buffered
 +</code>
 +
 +===== C Sourcecode Example =====
 +<code c>
 +/* 
 + * setvbuf example code
 + * http://code-reference.com/c/stdio.h/setvbuf
 + */
 +
 +#include <stdio.h> /* including standard library */
 +//#include <windows.h> /* uncomment this for Windows */
 +
 +
 +int main ( void )
 +{
 +  FILE *stream_not_buffered, *stream_full_buffered, *stream_line_buffered;
 +  stream_full_buffered=fopen ("test.txt","w");
 +  stream_not_buffered=fopen("test2.txt","w");
 +  stream_line_buffered=fopen("test3.txt","w");
 +  
 +/* case 1 full buffered */
 +  setvbuf ( stream_full_buffered , NULL , _IOFBF , 2000L );
 +
 +  fputs("Buffered input for setvbuf full buffered", stream_full_buffered);
 +  fflush(stream_full_buffered);
 +  fclose(stream_full_buffered);
 +
 +
 +/* case 2 not buffered */
 +  setvbuf ( stream_not_buffered , NULL , _IONBF , BUFSIZ );
 +
 +  fputs("input for setvbuf not buffered", stream_not_buffered);
 +  fflush(stream_not_buffered);
 +  fclose(stream_not_buffered);
 +
 +
 +/* case 3 line buffered */
 +  setvbuf ( stream_line_buffered , NULL , _IOLBF , 80 );
 +
 +  fputs("Buffered input for setvbuf line buffered", stream_line_buffered);
 +  fflush(stream_line_buffered);
 +  fclose(stream_line_buffered);
 +
 +
 +  return 0;
 +}
 +
 +</code>
 +
 +
 +    output: ./setvbuf
 +    
 +==== content of test.txt ===
 +<code c>
 +Buffered input for setvbuf full buffered
 +</code>
 +==== content of test2.txt ===
 +<code c>
 +input for setvbuf not buffered
 +</code>
 +==== content of test3.txt ===
 +<code c>
 +Buffered input for setvbuf line buffered
 +</code>
 +
 +
 +see also  [[c:stdio.h:setbuf|]]
  
advertising
c/stdio.h/setvbuf.txt · Last modified: 2013/01/22 22:02 (external edit)

Impressum Datenschutz