Compare the use of setbuf of function and setvbuf of function in C language

  • 2020-04-02 03:18:57
  • OfStack

C language setbuf() function: the buffer associated with the flow
The header file:


#include <stdio.h>

The setbuf() function is used to associate a specified buffer with a particular file stream, enabling you to manipulate the file stream directly while manipulating the buffer. The prototype is as follows:


void setbuf(FILE * stream, char * buf);

Stream is the pointer to the file stream and buf is the starting address of the buffer.


If the parameter buf is NULL, it is unbuffered, and setbuf() is equivalent to calling setvbuf(stream, buf, buf ? _IOFBF: _IONBF, BUFSIZE).

Note: after opening the file stream, setbuf() can be called to set the buffer for the file stream (and it must be) before the contents are read.

Observe the effect of a buffer being associated with a stream.


#include <stdio.h> 
char outbuf[BUFSIZ]; 
int main(void) 
{ 
  setbuf(stdout, outbuf); //Connect the buffer to the stream
  puts("This is a test of buffered output.n");
  puts(outbuf);
  fflush(stdout); //The refresh
  puts(outbuf); //The output
  return 0; 
}

Output results:


This is a test of buffered output..
This is a test of buffered output..
This is a test of buffered output..
This is a test of buffered output..

The program connects outbuf to the output stream, then outputs a string, which is also held in outbuf because the buffer is already attached to the stream, and then the puts function prints out again, so now there are two identical strings in outbuf. After the output stream is refreshed, puts again outputs two more strings.

C language setvbuf() function: set the buffer of the file stream
The header file:


 #include <stdio.h>

The function setvbuf() is used to set the buffer of the file stream.


  int setvbuf(FILE * stream, char * buf, int type, unsigned size);

[parameter] stream is the pointer to the file stream, buf is the first address of the buffer, type is the buffer type, and size is the number of bytes in the buffer.

The description of parameter type is as follows:

_IOFBF (full buffer) : reads data from the stream when the buffer is empty. Or when the buffer is full, write data to the stream. _IOLBF (row buffering) : reads one row at a time from the stream or writes - rows to the stream. _IONBF (unbuffered) : reads data directly from the stream or writes data directly to the stream without a buffer.

[return value] returns 0 on success, non-0 on failure.

This function covers knowledge of flows and buffers, refer to the topic of C flows and buffers (caching).

If you just want a simple buffer operation, you can also use the setbuf() function, see: C language setbuf() function

The practical significance of the setbuf() and setvbuf() functions is that when a user opens a file, he or she can create his or her own file buffer, instead of the default buffer that was set when the file was opened using the fopen() function. This allows the user to control the buffer by himself, including changing the buffer size, periodically flushing the buffer, changing the buffer type, deleting the default buffer in the stream, creating a buffer for the stream without a buffer, and so on.

Note: the call to setvbuf() can be used to set the buffer of the file stream (and must be) after the file stream is opened, before the contents are read.

Example: set a special type buffer for the file.


#include <stdio.h> 
int main(void) 
{ 
  FILE *input, *output; 
  char bufr[512]; 
  input = fopen("file.in", "w+");   
  output = fopen("file.out", "w");
  if (setvbuf(input, bufr, _IOFBF, 512) != 0) 
  {
   printf("failed to set up buffer for input filen");
  }
  else 
  {
   printf("buffer set up for input filen"); 
}
  if (setvbuf(output, NULL, _IOLBF, 132) != 0) 
  {
   printf("failed to set up buffer for output filen"); 
}
  else 
  {
   printf("buffer set up for output filen"); 
  }
  fclose(input); 
  fclose(output); 
  return 0; 
}

Operation results:


Buffer set up for input file
Buffer set up for output file

The program opens two files, sets the buffer separately, determines success based on the return value, and finally USES fclose to close the two files.


Related articles: