Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct Students
  4. {
  5. char name[15];
  6. int age;
  7. };
  8. void get_students(Students *array, int n);
  9.  
  10. int main()
  11. {
  12. FILE *file = fopen("file.bin", "w+b");
  13. if (!file)
  14. {
  15. perror("Unable to open file");
  16. exit(1);
  17. }
  18. int n;
  19. printf("How many students? \n > ");
  20. scanf("%d", &n);
  21.  
  22. Students* array = new Students[n];
  23.  
  24. get_students(array, n);
  25.  
  26. for (int i = 0; i < n; i++)
  27. fwrite(&array[i], sizeof(Students), 1, file);
  28.  
  29. for (int i = 0; i < n; i++)
  30. {
  31. fseek(file, sizeof(Students), SEEK_CUR);
  32. fread(&array[i], sizeof(int), 1, file);
  33. printf("%s %d\n", array[i].name, array[i].age);
  34. }
  35.  
  36. return 0;
  37. }
  38.  
  39. void get_students(Students *array, int n)
  40. {
  41. for (int i = 0; i < n; i++)
  42. {
  43. printf("\nName > ");
  44. scanf("%s", &array[i].name);
  45. printf("\n%s's age > ", array[i].name);
  46. scanf("%d", &array[i].age);
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement