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 28 29 30 31 32 33 34 35
| #include <stdio.h> #include <string.h>
#define NMEMB 3
struct student { char name[20]; int age; float height; }arr_stu[NMEMB];
void student_init(int idx, char *name, int age, float height) { strcpy(arr_stu[idx].name, name); arr_stu[idx].age = age; arr_stu[idx].height = height; }
int main() { FILE * stream;
student_init(0, "学生1", 11, 11.1); student_init(1, "学生2", 12, 11.2); student_init(2, "学生3", 13, 11.3);
stream = fopen("test", "w"); int wnum = fwrite(arr_stu, sizeof(struct student), NMEMB, stream); printf("write student info num %d!\n", wnum);
fclose(stream);
return 0; }
|