#include #include #include #include struct lista { char imie[20]; char nazwisko[20]; int wiek; struct lista *next; }; void dodaj(struct lista *dane) { struct lista *tmp; tmp=malloc(sizeof(struct lista)); char tmp1[20]; char tmp2[20]; printf("\n\nPodaj imie \n"); fgets(tmp1,20,stdin); strcpy(tmp->imie, tmp1); fflush(stdin); printf("Podaj nazwisko \n"); fgets(tmp2,20,stdin); strcpy(tmp->nazwisko, tmp2); fflush(stdin); printf("Podaj wiek \n"); scanf("%i",&tmp->wiek); fflush(stdin); tmp->next=NULL; while(dane->next != NULL){ dane=dane->next; } dane->next=tmp; } void zapisz(struct lista *dane,FILE *plik){ plik=fopen("test.txt","w"); while(dane->next != NULL) { fprintf(plik,"\n%s",dane->imie); fprintf(plik,"%s",dane->nazwisko); fprintf(plik,"%i",dane->wiek); dane=dane->next; } fprintf(plik,"\n%s",dane->imie); fprintf(plik,"%s",dane->nazwisko); fprintf(plik,"%i",dane->wiek); fclose(plik); } void show(struct lista *dane) { while(dane->next != NULL) { printf("%s\n", dane->imie); printf("%s\n", dane->nazwisko); printf("%i\n", dane->wiek); dane=dane->next; } printf("%s\n", dane->imie); printf("%s\n", dane->nazwisko); printf("%i\n", dane->wiek); } int main() { FILE *pliczek; struct lista *dane; dane = malloc(sizeof(struct lista)); char znak; strcpy(dane->imie,"Test"); strcpy(dane->nazwisko,"Testowe"); dane->wiek=20; dane->next=NULL; start: printf("-------------------------------------------------------------\nCo chcesz zrobic?\n\n1. Dodaj nowy element\n2. Wypisz elementy\n3.Zapisz elementy do pliku\n4.Zakoncz program\n"); znak=getch(); if(znak=='1') { fflush(stdin); dodaj(dane); goto start; } if(znak=='2') { fflush(stdin); show(dane); goto start; } if(znak=='3') { fflush(stdin); zapisz(dane,pliczek); goto start; } if(znak=='4') return 0; }