Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct Students
- {
- char name[15];
- int age;
- };
- void get_students(Students *array, int n);
- int main()
- {
- FILE *file = fopen("file.bin", "w+b");
- if (!file)
- {
- perror("Unable to open file");
- exit(1);
- }
- int n;
- printf("How many students? \n > ");
- scanf("%d", &n);
- Students* array = new Students[n];
- get_students(array, n);
- for (int i = 0; i < n; i++)
- fwrite(&array[i], sizeof(Students), 1, file);
- for (int i = 0; i < n; i++)
- {
- fseek(file, sizeof(Students), SEEK_CUR);
- fread(&array[i], sizeof(int), 1, file);
- printf("%s %d\n", array[i].name, array[i].age);
- }
- return 0;
- }
- void get_students(Students *array, int n)
- {
- for (int i = 0; i < n; i++)
- {
- printf("\nName > ");
- scanf("%s", &array[i].name);
- printf("\n%s's age > ", array[i].name);
- scanf("%d", &array[i].age);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement