Multiple Records


Explanation

In C, binary files can store multiple records efficiently using structures. In this program, the user enters details of multiple students, and each record is written to the binary file data.dat using the fwrite() function inside a loop.

How It Works?

  • Step 1: Create a structure to store student details.
  • Step 2: Declare an array of structures to store multiple student records.
  • Step 3: Open the binary file using fopen() in "wb" mode.
  • Step 4: Use a for loop to accept details of multiple students.
  • Step 5: Write each student record into the binary file using fwrite().
  • Step 6: Repeat until all records are stored.
  • Step 7: Close the file using fclose().

Example Program


#include <stdio.h>
#include <conio.h>

struct student
{

    char name[20];
    int rollno, fees;

};

main()
{

    struct student s[5];

    int k;

    FILE *fp;

    fp = fopen("data.dat", "wb");

    if(fp == NULL)
    {

        printf("Error opening file!");

        return;

    }

    for(k = 0; k <= 2; k++)
    {

        printf("Enter the Name : ");

        scanf("%s", s[k].name);

        printf("\n Enter Roll No. : ");

        scanf("%d", &s[k].rollno);

        printf("\n Enter the Fees : ");

        scanf("%d", &s[k].fees);

        fwrite(&s[k], sizeof(s[k]), 1, fp);

    }

    fclose(fp);

    getch();

}

Output

Enter the Name : Rahul
Enter Roll No. : 101
Enter the Fees : 5000

Enter the Name : Amit
Enter Roll No. : 102
Enter the Fees : 5500

Enter the Name : Neha
Enter Roll No. : 103
Enter the Fees : 6000


Binary File:

3 student records stored successfully in data.dat

Practice Exercises

Task 1: Write a C program to store the details of 3 employees in a binary file named employee.dat using an array of structures.

Accept the following details for each employee:
  • Employee ID
  • Employee Name
  • Employee Salary
Goal: Learn how to store multiple records in a binary file using an array of structures and the fwrite() function.

💡 Hint: Create an array of employee structures, open the file using fopen("employee.dat","wb"), use a for loop to accept details, and write each record using fwrite().
💡 Show Solution

#include <stdio.h>
#include <conio.h>

struct employee
{

    int id, salary;
    char name[20];

};

main()
{

    struct employee e[3];

    FILE *fp;

    int k;

    clrscr();

    fp = fopen("employee.dat", "wb");

    for(k = 0; k < 3; k++)
    {

        printf("Enter Employee ID : ");
        scanf("%d", &e[k].id);

        printf("\nEnter Employee Name : ");
        scanf("%s", e[k].name);

        printf("\nEnter Employee Salary : ");
        scanf("%d", &e[k].salary);

        fwrite(&e[k], sizeof(e[k]), 1, fp);

    }

    fclose(fp);

    getch();

}

Output
Enter Employee ID : 101
Enter Employee Name : Amit
Enter Employee Salary : 25000

Enter Employee ID : 102
Enter Employee Name : Rahul
Enter Employee Salary : 30000

Enter Employee ID : 103
Enter Employee Name : Neha
Enter Employee Salary : 28000


3 employee records stored successfully in employee.dat
Explanation: The program creates an array of employee structures and accepts details for three employees. Each record is stored in the binary file employee.dat using the fwrite() function inside a for loop. This allows multiple records to be stored efficiently in a binary file.