Buffered I/O in C Language
Introduction
Buffered I/O is a method of performing input and output operations in the C programming language. It involves using a buffer, which is a temporary storage area, to hold data before it is read or written. This technique helps in improving the efficiency of I/O operations by reducing the number of system calls required. In this tutorial, we will explore how buffered I/O works in C, its advantages, and how to implement it using various standard library functions.
Understanding Buffers
A buffer is a block of memory used to temporarily store data while it is being moved from one place to another. When using buffered I/O, data is first written to a buffer. Once the buffer is full or the operation is complete, the data is then transferred to the final destination (e.g., a file or a terminal).
Buffered I/O can be categorized into three types:
- Fully Buffered: Data is transferred when the buffer is full.
- Line Buffered: Data is transferred when a newline character is encountered.
- Unbuffered: Data is transferred immediately without buffering.
Standard I/O Library Functions
The C standard library provides several functions for performing buffered I/O operations. These functions are part of the stdio.h
header file. Some common functions include:
fopen
: Opens a file.fclose
: Closes a file.fread
: Reads data from a file.fwrite
: Writes data to a file.fflush
: Flushes the buffer.setbuf
andsetvbuf
: Sets the buffer for a stream.
Example: Writing to a File Using Buffered I/O
Let's look at an example of writing data to a file using buffered I/O in C:
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *file;
char buffer[256];
// Open file for writing
file = fopen("example.txt", "w");
if (file == NULL) {
perror("Error opening file");
return EXIT_FAILURE;
}
// Write data to buffer
snprintf(buffer, sizeof(buffer), "Hello, World!\n");
// Write buffer to file
if (fwrite(buffer, sizeof(char), sizeof(buffer), file) < sizeof(buffer)) {
perror("Error writing to file");
fclose(file);
return EXIT_FAILURE;
}
// Flush buffer to ensure data is written
fflush(file);
// Close file
fclose(file);
return EXIT_SUCCESS;
}
Example: Reading from a File Using Buffered I/O
Now, let's look at an example of reading data from a file using buffered I/O in C:
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *file;
char buffer[256];
// Open file for reading
file = fopen("example.txt", "r");
if (file == NULL) {
perror("Error opening file");
return EXIT_FAILURE;
}
// Read data from file into buffer
if (fread(buffer, sizeof(char), sizeof(buffer), file) < sizeof(buffer)) {
if (feof(file)) {
printf("End of file reached.\n");
} else {
perror("Error reading from file");
}
}
// Print data from buffer
printf("Data read from file: %s\n", buffer);
// Close file
fclose(file);
return EXIT_SUCCESS;
}
Advantages of Buffered I/O
Buffered I/O offers several advantages over unbuffered I/O:
- Improved Performance: By reducing the number of system calls, buffered I/O can significantly improve the performance of I/O operations.
- Reduced CPU Usage: Buffering helps in reducing the CPU usage by minimizing the overhead of frequent I/O operations.
- Better Resource Management: Efficient use of memory and other system resources.
Conclusion
Buffered I/O is an essential concept in C programming that helps in optimizing input and output operations. By using buffers, we can improve the performance and efficiency of our programs. This tutorial covered the basics of buffered I/O, including how to use standard library functions for reading and writing data, as well as the advantages of using buffered I/O. With this knowledge, you can now implement buffered I/O in your own C programs to achieve better performance.