Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- struct student
- {
- char nazwisko[20];
- char imie[20];
- int rok_std;
- int data_ur;
- };
- struct student *dodaj(struct student * ptr, int *i);
- void zapisz(struct student * ptr, int i);
- void drukuj(struct student ptr);
- void wypisz();
- int main()
- {
- struct student *specimen = NULL; //wskaźnik na początek tablicy
- int count = 0, i; //licznik elementów
- specimen = dodaj(specimen, &count);
- zapisz(specimen, count);
- free(specimen);
- wypisz();
- printf("\nRozmiar specimen: %d", sizeof(specimen));
- printf("\n");
- system("pause");
- }
- struct student * dodaj(struct student* ptr, int *i)
- {
- printf("\n***FUNKCJA BEDZIE DODAWAC KOLEJNE STRUKTURY DO TABLICY, ZEBY PRZERWAC 0***\n");
- int x=1;
- do
- {
- ptr = (struct student*)realloc(ptr, (*i + 1) * sizeof(struct student)); //alokacja pamieci
- fflush(stdin);
- printf("Podaj imie\n");
- fflush(stdin);
- scanf("%s", ptr[*i].imie); //to *i to indeks
- printf("Podaj nazwisko\n");
- fflush(stdin);
- scanf("%s", ptr[*i].nazwisko);
- printf("Podaj date urodznia\n");
- fflush(stdin);
- scanf("%d", &ptr[*i].data_ur);
- printf("Podaj rok studiow\n");
- fflush(stdin);
- scanf("%d", &ptr[*i].rok_std);
- *i+=1;
- printf("\nChcesz kontynuowac? \n");
- fflush(stdin);
- scanf("%d", &x);
- } while (x>0); //jesli podamy wartosc mniejsza od 1 to koniec
- return ptr;
- }
- void zapisz(struct student * ptr, int i)
- {
- FILE *plik;
- plik = fopen("dane.txt", "w");
- fwrite(ptr, sizeof(struct student), i, plik);
- fclose(plik);
- }
- void drukuj(struct student ptr)
- {
- printf("\nSTRUKTURA\n");
- printf("\nNazwisko: %s", ptr.nazwisko);
- printf("\nImie: %s", ptr.imie);
- printf("\nData urodzenia:: %d", ptr.data_ur);
- printf("\nImie: %d", ptr.rok_std);
- }
- void wypisz()
- {
- FILE *plik;
- struct student s;
- plik = fopen("dane.txt", "r");
- while (fread(&s, sizeof(struct student), 1, plik) == 1)
- drukuj(s);
- fclose(plik);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement