setbuf() Function
The setbuf()
function in C stdio.h sets the buffering mode for a file stream, either by assigning a user-defined buffer to the stream or by disabling buffering altogether. This function ensures that the stream becomes fully buffered when a valid buffer is provided, or unbuffered when a null pointer is specified, affecting how input/output operations are handled.
Syntax of setbuf()
void setbuf(FILE *stream, char *buffer);
Parameters
Parameter | Description |
---|---|
stream | Pointer to a FILE object that identifies an open stream. |
buffer | User-allocated buffer which must be at least BUFSIZ bytes in size, or a null pointer to disable buffering. |
This function should be called after a stream is associated with an open file but before any input or output operations are performed. If a valid buffer is provided, the stream becomes fully buffered, using the entire buffer; if a null pointer is passed, buffering is disabled and the stream becomes unbuffered. The behavior is equivalent to calling setvbuf()
with _IOFBF
and BUFSIZ
for a non-null buffer, or _IONBF
for a null pointer.
Return Value
setbuf()
does not return a value.
Examples for setbuf()
Example 1: Assigning a Custom Buffer to a File Stream
This example demonstrates how to assign a custom buffer to a file stream to enable full buffering. A user-defined buffer is used, and the stream operations will utilize this buffer.
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp = fopen("example.txt", "w");
if (fp == NULL) {
perror("Error opening file");
return EXIT_FAILURE;
}
char buf[BUFSIZ];
setbuf(fp, buf); /* Assign a custom buffer to fp */
fprintf(fp, "This is a test using a custom buffer.\n");
fclose(fp);
printf("Data written to file with custom buffering.\n");
return 0;
}
Explanation:
- The program opens a file “example.txt” for writing.
- A buffer
buf
of sizeBUFSIZ
is declared. setbuf()
is called to assign the custom buffer to the file stream, making it fully buffered.- Data is written to the file, and the stream is closed, which flushes the buffer.
- A confirmation message is printed on the console.
Program Output:
Data written to file with custom buffering.
Example 2: Disabling Buffering for a File Stream
This example illustrates how to disable buffering for a file stream by passing a null pointer to setbuf()
. With buffering disabled, every I/O operation is performed directly.
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp = fopen("nobuffer.txt", "w");
if (fp == NULL) {
perror("Error opening file");
return EXIT_FAILURE;
}
setbuf(fp, NULL); /* Disable buffering for fp */
fprintf(fp, "Buffering is disabled.\n");
fclose(fp);
printf("Data written to file without buffering.\n");
return 0;
}
Explanation:
- The file “nobuffer.txt” is opened for writing.
setbuf()
is called with a null pointer, disabling buffering for this stream.- The message “Buffering is disabled.” is directly written to the file.
- The stream is closed, ensuring that the data is immediately flushed.
- A confirmation message is printed on the console.
Example 3: Demonstrating Buffer Flushing with a Custom Buffer
This example shows how data is buffered and then flushed when using a custom buffer. It emphasizes the role of the buffer in reducing the number of physical I/O operations.
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp = fopen("flushbuffer.txt", "w");
if (fp == NULL) {
perror("Error opening file");
return EXIT_FAILURE;
}
char buf[BUFSIZ];
setbuf(fp, buf); /* Assign a custom buffer to the stream */
/* Write data to the file without explicitly flushing */
fprintf(fp, "Line 1: Buffered output.\n");
fprintf(fp, "Line 2: More buffered output.\n");
/* Flushing the stream to force write the buffered data */
fflush(fp);
fclose(fp);
printf("Buffered data flushed to file.\n");
return 0;
}
Explanation:
- The program opens a file “flushbuffer.txt” for writing.
- A custom buffer of size
BUFSIZ
is allocated and assigned to the file stream usingsetbuf()
. - Two lines of text are written to the file; these remain in the buffer until flushed.
fflush()
is called to flush the buffer, ensuring that all data is written to the file.- The stream is closed and a message is printed to indicate that the buffered data has been flushed.
Program Output:
Buffered data flushed to file.