Comparison of the use of SETBUF () functions and SETVBUF () functions in C language _c language

Source: Internet
Author: User
Tags comparison

C language Setbuf () function: to correlate a buffer with a stream
header file:

#include <stdio.h>

function Setbuf () is used to associate a specified buffer with a particular file stream, and to implement the ability to manipulate the file flow directly when manipulating the buffer. The prototype is as follows:

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

The parameter stream is the file stream pointer, and the BUF is the starting address of the buffer.


If the parameter is BUF to a null pointer, there is no buffer, and setbuf () is equivalent to calling Setvbuf (stream, buf, buf? _IOFBF: _IONBF, BufSize).

Note: After you open the file stream, you can call Setbuf () to set the buffer for the file stream (and this must be the case) before reading the content.

"Instance" observes the effect of the buffer with the Stream association.

#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); Refresh
  puts (OUTBUF);//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 first connects the OUTBUF to the output stream, and then outputs a string, because the buffer is already connected to the stream, so the string is stored in the OUTBUF, and then the puts function is output again, so now the OUTBUF is holding two strings. After refreshing the output stream, puts again, then output two strings.

C language Setvbuf () function: Set buffer for file stream
header file:

 #include <stdio.h>

The function setvbuf () is used to set the buffer for the file stream, and its prototype is:

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

The parameter stream is a file stream pointer, buf is the buffer's first address, type is the buffer type, and the size is the number of bytes in the buffer.

Type of parameter is described as follows:

    • _IOFBF (full buffer): reads data from the stream when the buffer is empty. Or writes data to the stream when the buffer is full.
    • _IOLBF (Row buffering): reads a row of data from the stream or writes to the stream.
    • _IONBF (no buffering): reads data directly from the stream or writes data directly to the stream without a buffer.

Return value successfully returns 0, and failure returns non-0.

This function involves the knowledge of the stream and the buffer, refer to the C language stream and the buffer (cache) topic.

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

The real meaning of the setbuf () and setvbuf () functions is that when a user opens a file, it can establish its own file buffer without having to use the default buffer set when the fopen () function opens the file. This allows the user to control the buffer itself, including changing the buffer size, timing flushing the buffer, changing the buffer type, deleting the default buffer in the stream, and opening the buffer for the stream without the buffer.

Note: After the file stream is opened, the call to Setvbuf () can be used to set the buffer for the file stream (and it must be) before the content is read.

Instance sets a special type buffer for the file.

#include <stdio.h> 
int main (void) 
{ 
  FILE *input, *output; 
  Char bufr[512]; 
  input = fopen ("File.In", "w+");   * * Open File
  /output = fopen ("File.out", "w");
  if (setvbuf (input, BUFR, _IOFBF, 512)!= 0)/* Failed/
  {
   printf ("Failed to set up buffer for input file\n");
  }
  Else 
  {
   printf ("Buffer set up for input file\n"); 
}
  if (setvbuf (output, NULL, _IOLBF, 132)!= 0)//* Specify a special buffer for the stream *
   /{printf ("failed to set up buffer for output file\n" ); 
}
  else 
  {
   printf ("Buffer set up for output file\n"); 
  }
  Fclose (input); 
  fclose (output); 
  return 0; 
}

Run Result:

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

The program first opens two files, sets the buffer separately, determines whether it succeeds based on the return value, and then closes the two files using the Fclose function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.