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;
- }
- }
- 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 add(Lista l, Dane d){
- if(!(l->head)){
- l->head = d;
- } else {
- l->last->next = d;
- }
- l->last = d;
- }
- Dane makeDane(const char *imie, int id){
- Dane d = (Dane)malloc(sizeof(struct dane));
- d->id = id;
- strcpy(d->imie, imie);
- d->next = NULL;
- return d;
- }
- void addDanemain(Lista l){
- char imie[20];
- int id = findId(l);
- puts("imie?");
- gets(imie);
- add(l, makeDane(imie,id));
- }
- void save(Lista l){
- FILE * plik = fopen("imiona.db", "wb");
- int licznik = zliczaj(l);
- fwrite(&licznik,sizeof(int),1,plik);
- Dane d = l->head;
- while(d){
- fwrite(d,sizeof(struct dane), 1, plik);
- d=d->next;
- }
- fclose(plik);
- }
- void load(Lista l){
- FILE * plik = fopen("imiona.db", "rb");
- if(!plik){
- return;
- }
- int licznik;
- fread(&licznik,sizeof(int),1,plik);
- printf("%d",licznik);
- getchar();
- while(licznik > 0){
- Dane d = (Dane)malloc(sizeof(struct dane));
- fread(d,sizeof(struct dane),1,plik);
- add(l,d);
- licznik--;
- }
- fclose(plik);
- }
- int main(){
- Lista studenci = makeList();
- load(studenci);
- printList(studenci);
- getchar();
- int i;
- for(i=0;i<5;i++){
- addDanemain(studenci);
- }
- printList(studenci);
- save(studenci);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment