setbuf()

Changes a stream's buffer.

  #include <stdio.h>
  
  void setbuf(FILE *stream, char *buf);    
Parameter

buf

A pointer to a new buffer.

stream

A pointer to a file stream.

Remarks

The setbuf() function allows the programmer to set the buffer for stream. It should be called after stream is opened, but before it is read from or written to.

The function makes the array pointed to by buf the buffer used by stream. The buf argument can either be a null pointer or point to an array of size BUFSIZ defined in stdio.h.

If buf is a null pointer, the stream becomes unbuffered.

This facility may not be available on configurations of the EWL that run on platforms without file systems.

Listing: Example of setbuf() usage

#include <stdio.h>

#include <stdlib.h>

int main(void)

{

FILE *f;

char name[80];

// Get a file name from the user

printf("Enter the name of the file to write to.\n");

gets(name);

// Create a new file for output

if ( (f = fopen(name, "w")) == NULL) {

printf("Can't open file %s.\n", name);

exit(1);

}

setbuf(f, NULL); // turn off buffering

// This text is sent directly to the file without

// buffering

fprintf(f, "Buffering is now off\n");

fprintf(f, "for this file.\n");

// close the file

fclose(f);

return 0;

}

Output:

Enter the name of the file to write to.

bufftest