Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.94 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. struct student
  5. {
  6.     char nazwisko[20];
  7.     char imie[20];
  8.     int rok_std;
  9.     int data_ur;
  10. };
  11.  
  12. struct student *dodaj(struct student * ptr, int *i);
  13. void zapisz(struct student * ptr, int i);
  14. void drukuj(struct student  ptr);
  15. void wypisz();
  16.  
  17.  
  18.  
  19. int main()
  20. {
  21.     struct student *specimen = NULL; //wskaźnik na początek tablicy
  22.     int count = 0, i; //licznik elementów
  23.  
  24.     specimen = dodaj(specimen, &count);
  25.     zapisz(specimen, count);
  26.  
  27.     free(specimen);
  28.     wypisz();
  29.     printf("\nRozmiar specimen: %d", sizeof(specimen));
  30.  
  31.     printf("\n");
  32.     system("pause");
  33.    
  34. }
  35.  
  36.  
  37. struct student * dodaj(struct student* ptr, int *i)
  38. {
  39.     printf("\n***FUNKCJA BEDZIE DODAWAC KOLEJNE STRUKTURY DO TABLICY, ZEBY PRZERWAC 0***\n");
  40.     int x=1;
  41.    
  42.     do
  43.     {
  44.         ptr = (struct student*)realloc(ptr, (*i + 1) * sizeof(struct student)); //alokacja pamieci
  45.         fflush(stdin);
  46.         printf("Podaj imie\n");
  47.         fflush(stdin);
  48.         scanf("%s", ptr[*i].imie); //to *i to indeks
  49.         printf("Podaj nazwisko\n");
  50.         fflush(stdin);
  51.         scanf("%s", ptr[*i].nazwisko);
  52.         printf("Podaj date urodznia\n");
  53.         fflush(stdin);
  54.         scanf("%d", &ptr[*i].data_ur);
  55.         printf("Podaj rok studiow\n");
  56.         fflush(stdin);
  57.         scanf("%d", &ptr[*i].rok_std);
  58.  
  59.         *i+=1;
  60.         printf("\nChcesz kontynuowac? \n");
  61.         fflush(stdin);
  62.         scanf("%d", &x);
  63.     } while (x>0); //jesli podamy wartosc mniejsza od 1 to koniec
  64.     return ptr;
  65. }
  66.  
  67. void zapisz(struct student * ptr, int i)
  68. {
  69.     FILE *plik;
  70.     plik = fopen("dane.txt", "w");
  71.     fwrite(ptr, sizeof(struct student), i, plik);
  72.     fclose(plik);
  73. }
  74. void drukuj(struct student  ptr)
  75. {
  76.     printf("\nSTRUKTURA\n");
  77.     printf("\nNazwisko: %s", ptr.nazwisko);
  78.     printf("\nImie: %s", ptr.imie);
  79.     printf("\nData urodzenia:: %d", ptr.data_ur);
  80.     printf("\nImie: %d", ptr.rok_std);
  81.  
  82. }
  83.  
  84. void wypisz()
  85. {
  86.     FILE *plik;
  87.     struct student s;
  88.     plik = fopen("dane.txt", "r");
  89.     while (fread(&s, sizeof(struct student), 1, plik) == 1)
  90.         drukuj(s);
  91.     fclose(plik);
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement