When working with C programming, input/output (I/O) operations are essential for interacting with files and streams. While formatted I/O functions like printf() and scanf() are commonly used, unformatted I/O functions such as fread() and fwrite() offer greater efficiency and flexibility for handling raw binary data. In this blog post, we will explore unformatted I/O in C, how it works, and how you can use it effectively in your programs.

Command line execution

Terminalbash
sed for unformatted I/O in C are  fread()  and  fwrite() . fread() : Reads a block of data from a file. fwrite() : Writes a block of data to a file. Understanding fread() and fwrite() The  fread()  and  fwrite()  functions are part of the standard I/O library ( stdio.h ) and are used for unformatted binary input and output. Syntax: size_t fread(void *ptr, size_t size, size_t count, FILE *stream); size_t fwrite(const void *ptr, size_t size, size_t count, FILE *stream); ptr : Pointer to a block of memory where the data will be stored or read from. size : Size of each element to be read or written. count : Number of elements to be read or written. stream : Pointer to the  FILE  object that identifies the file. Both functions return the number of elements successfully read or written. Example: Writing and Reading Binary Data Let’s look at a practical example where we use  fwrite()  to write data to a binary file and  fread()  to read it back. Writing Data to a Binary File: #include <stdio.h> struct Data {     int id;     char name[20]; }; int main() {     FILE *filePointer;     struct Data record = {1, "John Doe"};     filePointer = fopen("data.bin", "wb");     if (filePointer == NULL) {         printf("Error: Could not open file.\n");         return 1;     }     fwrite(&record, sizeof(struct Data), 1, filePointer);     fclose(filePointer);     return 0; } Reading Data from a Binary File: #include <stdio.h> struct Data {     int id;     char name[20]; }; int main() {     FILE *filePointer;     struct Data record;     filePointer = fopen("data.bin", "rb");     if (filePointer == NULL) {         printf("Error: Could not open file.\n");         return 1;     }     fread(&record, sizeof(struct Data), 1, filePointer);     printf("ID: %d, Name: %s\n", record.id, record.name);     fclose(filePointer);     return 0; } Explanation: Writing Binary Data : The  fwrite()  function writes the binary representation of the  Data  structure to the file  data.bin . Reading Binary Data : The  fread()  function reads the binary data from  data.bin  into the  record  structure. Advantages of Unformatted I/O Efficiency : Unformatted I/O is faster since it avoids the overhead of formatting and conversion. Exact Data Representation : It preserves the exact binary representation of data, which is crucial for binary files. Compact Storage : Binary files are generally smaller than text files because they do not include formatting characters. Practical Applications of Unformatted I/O Saving and Loading Data Structures : Unformatted I/O is ideal for saving and loading complex data structures in binary form. Image and Audio Processing : It is commonly used in applications that handle raw image, audio, and video data. Database Storage : Storing records in a binary file allows for fast retrieval and efficient storage. Best Practices for Using Unformatted I/O Error Checking : Always check the return value of  fread()  and  fwrite()  to ensure the correct number of elements were read or written. Data Consistency : Ensure that the structure of data in memory matches the structure expected in the file. Endianness Awareness : Be mindful of the endianness of the system when dealing with binary data, especially if the file will be shared across different platforms. Common Mistakes to Avoid Incorrect Size or Count : Miscalculating the  size  or  count  parameters can lead to incomplete data reads or writes. Not Closing Files : Always close the file with  fclose()  to prevent data corruption and memory leaks. Ignoring Endianness : Failing to account for endianness differences can result in corrupted data when reading or writing binary files on different systems. Unformatted I/O in C provides a powerful and efficient way to handle raw binary data, making it a critical tool for developers working with binary files, complex data structures, and performance-sensitive applications. By mastering  fread()  and  fwrite() , you can ensure that your programs are capable of efficient data storage and retrieval, while maintaining data integrity and performance.

Implementation details

What is Unformatted I/O in C? Unformatted I/O in C refers to reading and writing data in its raw binary form, without any conversion or formatting. This is particularly useful for working with binary files, where data needs to be stored and retrieved exactly as it is in memory. The primary functions used for unformatted I/O in C are fread() and fwrite() . fread() : Reads a block of data from a file.

Gotchas and common issues

  • Permission checks - verify user access rights and sudo privileges before executing system-level operations.

  • Environment configuration - double-check path variables and dependency versions to prevent runtime failures.

  • Backup safeguards - maintain configuration backups before applying system or database modifications.

Following these steps ensures clean configuration and reliable execution for mastering unformatted i/o in c: efficient data handling with fread() and fwrite().