C File Buffering 📜

beginner
23 min

C File Buffering 📜

Welcome to our deep dive into C File Buffering! In this lesson, you'll learn how to manage input and output operations efficiently using buffers in C programming. Let's get started! 🎯

Understanding Buffers 📝

A buffer is a region of memory that holds data temporarily. In the context of C programming, buffers are used for managing input and output operations to improve performance.

Buffered and Unbuffered Input/Output 💡

In C, by default, all input and output operations are unbuffered. This means that each read or write operation is done as soon as it is requested. Buffered input/output, on the other hand, collects data before sending it, reducing the number of system calls and improving performance.

The fopen() Function 📝

The fopen() function is used to open a file for reading (r), writing (w), or appending (a) in C.

c
FILE *fp = fopen("example.txt", "w");

In this example, example.txt is the file we're working with, and "w" stands for write mode. The function returns a FILE pointer, which we store in fp.

Buffered Output with fprintf() 💡

To write buffered output, use the fprintf() function. By default, fprintf() uses a buffer.

c
fprintf(fp, "Hello, World!\n");

In this example, we're writing "Hello, World!" to the file opened with fopen().

Flushing the Buffer 💡

When you're done writing to a file, it's essential to flush the buffer to ensure all data is written to the file. You can do this using the fflush() function.

c
fflush(fp);

In this example, we're flushing the buffer associated with the file opened using fopen().

Buffered Input with fgets() 💡

To read buffered input, use the fgets() function.

c
char str[100]; fgets(str, sizeof(str), fp);

In this example, we're reading a line from the file into the str array.

Closing the File 📝

Once you're done with the file, make sure to close it using the fclose() function.

c
fclose(fp);

In this example, we're closing the file opened using fopen().

Advanced Example: Reading and Writing to a File 💡

Let's put it all together in an example that reads a line from a file and writes it back, but with all characters reversed.

c
#include <stdio.h> int main() { FILE *fp = fopen("example.txt", "r"); char str[100]; char rev_str[100]; // Read a line from the file fgets(str, sizeof(str), fp); // Reverse the string int i = 0, j = strlen(str) - 1; while (i < j) { char temp = str[i]; str[i] = str[j]; str[j] = temp; i++; j--; } // Write the reversed string back to the file FILE *fp_out = fopen("example_reversed.txt", "w"); fprintf(fp_out, "%s", str); fclose(fp_out); fclose(fp); return 0; }

In this example, we open the file example.txt, read a line using fgets(), reverse the string, and write the reversed string to a new file named example_reversed.txt.

Quiz 🎯

Quick Quiz
Question 1 of 1

What does the `fprintf()` function do?

That's it for our C File Buffering lesson! With this knowledge, you're ready to handle input and output operations more efficiently in your C programs. 🚀 Happy coding! 💡