setbuf Function

Defines how a stream is buffered.

Include

<stdio.h>

Prototype

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

Arguments

stream pointer to the open stream
buf user allocated buffer

Remarks

setbuf must be called after fopen but before any other function calls that operate on the stream. If buf is a null pointer, setbuf calls the function setvbuf(stream, 0, _IONBF, BUFSIZ) for no buffering; otherwise setbuf calls setvbuf(stream, buf, _IOFBF, BUFSIZ) for full buffering with a buffer of size BUFSIZ. See setvbuf.

This function requires a heap.

Example

#include <stdio.h> /* for setbuf, printf, */
                   /* fopen, fclose,      */
                   /* FILE, NULL, BUFSIZ  */

int main(void)
{
  FILE *myfile1, *myfile2;
  char buf[BUFSIZ];

  if ((myfile1 = fopen("afile1", "w+")) != NULL)
  {
    setbuf(myfile1, NULL);
    printf("myfile1 has no buffering\n");
    fclose(myfile1);
  }

  if ((myfile2 = fopen("afile2", "w+")) != NULL)
  {
    setbuf(myfile2, buf);
    printf("myfile2 has full buffering");
    fclose(myfile2);
  }

}

Example Output

myfile1 has no buffering
myfile2 has full buffering

This macro is used by the function setbuf.

BUFSIZ

Defines the size of the buffer used.

Include

<stdio.h>

Value

512