C Program to Write on Data File and Read From Data File
Program : Write on the Data File
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #include<stdio.h> struct Student { int roll; char name[12]; int percent; } s1 = { 10, "SMJC", 80 }; int main() { FILE *fp; struct Student s2; //Write details of s1 to file fp = fopen("ip.txt", "w"); fwrite(&s1, sizeof(s1), 1, fp); fclose(fp); fp = fopen("ip.txt", "r"); fread(&s2, sizeof(s2), 1, fp); fclose(fp); printf("\nRoll : %d", s2.roll); printf("\nName : %s", s2.name); printf("\nPercent : %d", s2.percent); return (0); } |
Output :
1 2 3 | Roll : 10 Name : SMJC Percent : 80 |