Guest User

Untitled

a guest
Apr 22nd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. #include "highscores.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. int aantal;
  7. Score_Entry* scores;
  8. int veranderd=0;
  9.  
  10. void load_highscores()
  11. {
  12. FILE* file=fopen(HIGHSCORE_FILE,"r");
  13. if(file!=NULL){
  14. fseek(file,0,SEEK_END);
  15. aantal=ftell(file)/sizeof(Score_Entry);
  16. fseek(file,0,SEEK_SET);
  17. if(aantal<MAX_HIGHSCORE_ENTRIES){
  18. scores=(Score_Entry*)calloc(aantal+1,sizeof(Score_Entry));
  19. fread(scores,sizeof(Score_Entry),aantal+1,file);
  20. }
  21. else if(aantal==5){
  22. scores=(Score_Entry*)calloc(aantal,sizeof(Score_Entry));
  23. fread(scores,sizeof(Score_Entry),aantal,file);
  24. }
  25. fclose(file);
  26. }
  27. else{
  28. aantal=0;
  29. scores=(Score_Entry*)calloc(1,sizeof(Score_Entry));
  30. }
  31. }
  32.  
  33. void check_highscore_entry(int score){
  34. if(aantal<MAX_HIGHSCORE_ENTRIES){
  35. aantal++;
  36. veranderd=1;
  37. }
  38. else if(score>scores[MAX_HIGHSCORE_ENTRIES-1].score){
  39. veranderd=1;
  40. }
  41. if(veranderd){
  42. int i=1;
  43. const time_t tijd=time(NULL);
  44. strftime(scores[aantal-1].datum,20,"%d/%m/%Y",localtime(&tijd));
  45. printf("Naam?\n");
  46. scanf("%s",scores[aantal-1].naam);
  47. scores[aantal-1].score=score;
  48. while(scores[aantal-i].score>scores[aantal-(i+1)].score&&(i+1<=aantal)){
  49. Score_Entry temp=scores[aantal-(i+1)];
  50. scores[aantal-(i+1)]=scores[aantal-i];
  51. scores[aantal-i]=temp;
  52. i++;
  53. }
  54. }
  55. }
  56.  
  57. void display_highscores()
  58. {
  59. int i;
  60. for(i=0;i<aantal;i++){
  61. printf("%d, %s %s\n",scores[i].score,scores[i].naam,scores[i].datum);
  62. }
  63. }
  64.  
  65. void save_highscores()
  66. {
  67. if(veranderd){
  68. FILE* file=fopen(HIGHSCORE_FILE,"w");
  69. fwrite(scores,sizeof(Score_Entry),aantal,file);
  70. veranderd=0;
  71. fclose(file);
  72. free(scores);
  73. }
  74. }
Add Comment
Please, Sign In to add comment