Q:

Writing an array of structure in a file using the fwrite in C

belongs to collection: File handling in C

0

Writing an array of  structure in a file using the fwrite in C

All Answers

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

#include <stdio.h>
typedef struct
{
    int id;
    char fName[16];
    char lName[16];
} s_employee;
int main()
{
    //Populate variable of array of structure
    s_employee sAticleworldEmplInfo[] =
    {
        {1, "Amlendra", "Mishra"},
        {2, "Pooja", "Mishra"},
        {3, "Apoorv", "Mishra"},
        {4, "Amitosh", "Mishra"},
        {5, "Maya", "Mishra"},
        {6, "Uday", "Mishra"},
    };
    //file pointer
    FILE *fp = NULL;
    //create and open the text file
    fp = fopen("aticleworld.dat", "wb");
    if(fp == NULL)
    {
        printf("Error in creating the file\n");
        exit(1);
    }
    //write the structure array in file
    fwrite(sAticleworldEmplInfo, sizeof(sAticleworldEmplInfo),1, fp);
    fclose(fp);
    return 0;
}

Output:

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

total answers (1)

create a file using fopen in C... >>
<< Writing a structure in a file using the fwrite in ...