Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stddef.h>
- typedef struct dane * Dane;
- struct dane {
- char imie[20];
- int id;
- Dane next;
- };
- typedef struct lista * Lista;
- struct lista {
- Dane head;
- Dane last;
- };
- int findId(Lista l){
- if(!(l->head)){
- return 1;
- } else {
- return (l->last->id) + 1;
- }
- }
- void addDane(Lista l){
- int id;
- char imie[20];
- Dane d = (Dane)malloc(sizeof(struct dane));
- id = findId(l);
- puts("imie?");
- gets(imie);
- strcpy(d->imie, imie);
- d->id = id;
- d->next = NULL;
- if(!(l->head)){
- l->head = d;
- } else {
- l->last->next = d;
- }
- l->last = d;
- }
- Lista makeList(){
- Lista l = (Lista)malloc(sizeof(struct lista));
- l->head = NULL;
- l->last = NULL;
- return l;
- }
- void printList(Lista l){
- Dane d = l->head;
- while(d){
- printf("%d. %s\n",d->id, d->imie);
- d=d->next;
- }
- }
- int zliczaj(Lista l){
- Dane d = l->head;
- int licznik = 0;
- while(d){
- licznik++;
- d=d->next;
- }
- return licznik;
- }
- void wczytaj(Lista l){
- int licz;
- FILE * plik = fopen("imiona.db", "rb");
- fread(&licz,sizeof(int),1, plik);
- Dane d = l->head;
- while(licz > 0){
- addDane(l);
- d=d->next;
- licz--;
- }
- fclose(plik);
- }
- void zapisz(Lista l){
- int licz = zliczaj(l);
- FILE * plik = fopen("imiona.db", "wb");
- fwrite(&licz,sizeof(int), 1, plik);
- Dane d = l->head;
- while(licz > 0){
- fwrite(d,sizeof(struct dane), 1, plik);
- d=d->next;
- licz--;
- }
- fclose(plik);
- }
- int main(){
- Lista studenci = makeList();
- wczytaj(studenci);
- int i;
- for(i=0;i<5;i++){
- addDane(studenci);
- }
- printList(studenci);
- zapisz(studenci);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment