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.
C Programming Language File Input/Output (Article) File Input/Output (Program)
9994Program:
#include 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.
This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.