Q:

Writing a structure in a file using the fwrite in C

belongs to collection: File handling in C

0

Writing a 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

The below code writes the id, first name and last name of the employee using the fwrite into the file.

#include <stdio.h>
typedef struct
{
    int id;
    char fName[16];
    char lName[16];
} s_employee;
int main()
{
    //Populate structure variable
    s_employee sAmlendraInfor =  {8886, "Amlendra", "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 in file
    fwrite(&sAmlendraInfor, sizeof(sAmlendraInfor),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)

Writing an array of structure in a file using the... >>
<< C program to write an array in a file using the fw...