Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct record
- {
- char name[30];
- int score;
- } record;
- int score = 23;
- char winner[30] = "gracz";
- typedef struct el_list
- {
- record record;
- struct el_list* next;
- } el_list;
- el_list *first = NULL;
- int addrecord()
- {
- el_list *record;
- record = (el_list*) malloc (sizeof(el_list));
- record->next = NULL;
- record->record.score = score;
- strcpy(record->record.name, winner);
- return record;
- }
- void addtolist(el_list** first)
- {
- el_list *pom, *tmp = addrecord();
- if (*first == NULL)
- *first = tmp;
- else if ((*first)->record.score > tmp->record.score)
- {
- tmp->next = *first;
- *first = tmp;
- }
- else
- {
- pom = (*first);
- while((pom->next != NULL) && (pom->record.score < tmp->record.score))
- pom = pom->next;
- tmp->next = pom->next;
- pom->next = tmp;
- }
- }
- void save2file(el_list* first)
- {
- el_list *tmp;
- FILE *hs = fopen("highscores.txt", "w");
- if( hs == NULL)
- perror("Blad z plikiem.");
- else
- {
- tmp = first;
- while(tmp != NULL)
- {
- fprintf( hs, "%d %s\n", score, winner);
- tmp = tmp->next;
- }
- }
- fclose(hs);
- }
- int main()
- {
- addtolist(&first);
- save2file(&first);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment