Explain the functions fread() and fwrite() used in files in C

In C programming, fread() and fwrite() functions are used for binary file operations to read and write entire structures or data blocks at once. These functions are particularly useful when working with structured data like records.

Syntax

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);

Parameters

  • ptr − Pointer to the memory location to read/write data
  • size − Size of each element in bytes
  • nmemb − Number of elements to read/write
  • stream − File pointer

Return Value

Both functions return the number of items successfully read or written. If this number differs from nmemb, either an error occurred or end-of-file was reached.

Example: Student Records with fwrite() and fread()

Note: This example demonstrates file operations. In an online compiler, the file operations may not persist, but the code will compile and show the structure.
#include <stdio.h>
#include <string.h>

struct student {
    int sno;
    char sname[30];
    float marks;
};

int main() {
    struct student s[2];
    FILE *fp;
    int i;

    /* Writing data to file */
    fp = fopen("student.dat", "wb");
    if (fp == NULL) {
        printf("Error opening file for writing<br>");
        return 1;
    }

    /* Input student data */
    for (i = 0; i < 2; i++) {
        printf("Enter details of student %d:<br>", i + 1);
        printf("Student number: ");
        scanf("%d", &s[i].sno);
        printf("Student name: ");
        scanf("%s", s[i].sname);
        printf("Student marks: ");
        scanf("%f", &s[i].marks);

        /* Write structure to file */
        fwrite(&s[i], sizeof(struct student), 1, fp);
    }
    fclose(fp);

    /* Reading data from file */
    fp = fopen("student.dat", "rb");
    if (fp == NULL) {
        printf("Error opening file for reading<br>");
        return 1;
    }

    printf("\nStudent details from file:<br>");
    for (i = 0; i < 2; i++) {
        /* Read structure from file */
        fread(&s[i], sizeof(struct student), 1, fp);
        printf("Student %d:<br>", i + 1);
        printf("Number: %d<br>", s[i].sno);
        printf("Name: %s<br>", s[i].sname);
        printf("Marks: %.2f<br><br>", s[i].marks);
    }
    fclose(fp);

    return 0;
}

Key Points

  • Use "wb" mode for writing binary data and "rb" for reading binary data
  • Always check if file opening was successful before performing operations
  • fwrite() writes the exact binary representation of data
  • fread() reads data back in the same binary format
  • Always close files using fclose() after operations

Conclusion

The fread() and fwrite() functions provide efficient binary file I/O for structured data in C. They are ideal for storing and retrieving records while maintaining data integrity and performance.

Updated on: 2026-03-15T13:47:38+05:30

24K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements