Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct elem
  5. {
  6. char nume[20];
  7. int varsta;
  8. }A[20];
  9.  
  10. int varsta(struct elem A[20],int i)
  11. {
  12. if(A[i].varsta>18) return 1;
  13. return 0;
  14. }
  15.  
  16. void functie()
  17. {
  18. FILE *f;
  19. f=fopen("struct.bin","rb");
  20. struct elem *B;
  21. int i,n;
  22. fread(&n,1,1,f);printf("\n%d\n",n);
  23. B=(struct elem *)malloc(n*sizeof(struct elem));
  24. for(i=1;i<=n;i++)
  25. fread(&B[i],sizeof(struct elem),1,f);
  26. for(i=1;i<=n;i++)
  27. if(varsta(B,i)==1) printf("%s- cu varsta %d",B[i].nume,B[i].varsta);
  28. fclose(f);
  29. }
  30.  
  31. int main()
  32. {
  33. struct elem A[20];
  34. FILE *f;
  35. f=fopen("struct.bin","wb");
  36. int n,nr,i;
  37. printf("Numarul de persoane: ");scanf("%d",&n);
  38. fwrite(&n,1,1,f);
  39. for(i=1;i<=n;i++)
  40. {
  41. printf("Nume: ");scanf("%s",A[i].nume);
  42. printf("Varsta: ");scanf("%d",&A[i].varsta);
  43. fwrite(&A[i],sizeof(struct elem),1,f);
  44. }
  45. fclose(f);
  46. functie();
  47. return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement