Q:

Reading an array of structure using fread in C

belongs to collection: File handling in C

0

Reading an array of structure using fread in C

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

The below code using the fread function reads the first 5 elements of structure array from the file and stores it in the structure array sAticleworldEmplInfo.

#include <stdio.h>

//structure
typedef struct
{
    int id;
    char fName[16];
    char lName[16];
} s_employee;

int main()
{
    //Array of structure variable
    s_employee sAticleworldEmplInfo [10]=  {0};
    //file pointer
    FILE *fp = NULL;

    //open the existing file
    fp = fopen("aticleworld.dat", "rb");
    if(fp == NULL)
    {
        printf("Error in creating the file\n");
        exit(1);
    }
    //Reads the five first element of the array of structure
    fread(sAticleworldEmplInfo, sizeof(sAticleworldEmplInfo),5, fp);

    fclose(fp);

    return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

How to use fputc in C programming... >>
<< C Program to read an array from the file using the...