Home / Programs / Write a C program to write all the members of an array of structures to a file using fwrite(). Read the array from the file and display on the screen.
Programming Example

Write a C program to write all the members of an array of structures to a file using fwrite(). Read the array from the file and display on the screen.

👁 10,269 Views
💻 Practical Program
📘 Step by Step Learning
Write a C program to write all the members of an array of structures to a file using fwrite(). Read the array from the file and display on the screen.

Program Code

#include <stdio.h>
struct student
{
   char name[50];
   int height;
};
int main(){
    struct student stud1[5], stud2[5];   
    FILE *fptr;
    int i;

    fptr = fopen("file.txt","wb");
    for(i = 0; i < 5; ++i)
    {
        fflush(stdin);
        printf("Enter name: ");
        gets(stud1[i].name);

        printf("Enter height: "); 
        scanf("%d", &stud1[i].height); 
    }

    fwrite(stud1, sizeof(stud1), 1, fptr);
    fclose(fptr);

    fptr = fopen("file.txt", "rb");
    fread(stud2, sizeof(stud2), 1, fptr);
    for(i = 0; i < 5; ++i)
    {
        printf("Name: %s\nHeight: %d", stud2[i].name, stud2[i].height);
    }
    fclose(fptr);
}

Output

See Your File Directory

Explanation

Write a C program to write all the members of an array of structures to a file using fwrite(). Read the array from the file and display on the screen.

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.