Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.40 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct PlayerScore {
  5.     char name[20];
  6.     int score;
  7.     struct PlayerScore *next;
  8. };
  9. void newStruct(struct PlayerScore **currentPtr)
  10. {
  11.     (*currentPtr)->next = malloc(sizeof(struct PlayerScore));
  12.     (*currentPtr) = (*currentPtr)->next;
  13.     (*currentPtr)->next = NULL;
  14. }
  15. void FreeMemory(struct PlayerScore **currentPtr)
  16. {
  17.     struct PlayerScore *temp = NULL;
  18.  
  19.     while ((*currentPtr) != NULL) {
  20.         temp = (*currentPtr);
  21.         (*currentPtr) = (*currentPtr)->next;
  22.         free(temp);
  23.     }
  24. }
  25. void Input(struct PlayerScore **current)
  26. {
  27.    
  28.     printf("INPUT NAME!: ");
  29.     fgets((*current)->name, 20, stdin);
  30.     (*current)->name[strlen((*current)->name) - 1] = '\0';
  31.     (*current)->score = 100;
  32. }
  33. void OutPutt(struct PlayerScore **currentPtr, struct PlayerScore **tailPtr)
  34. {
  35.     while ((*currentPtr) != NULL) {
  36.        
  37.         printf("\nName: %s\nScore: %d\n", (*currentPtr)->name, (*currentPtr)->score);
  38.  
  39.         if ((*currentPtr)->next == NULL){
  40.             (*tailPtr) = (*currentPtr);
  41.         }
  42.  
  43.         (*currentPtr) = (*currentPtr)->next;
  44.     }
  45. }
  46. int openFile(FILE **textFilePtr, char wordPtr[13], fpos_t *textFilePos)
  47. {
  48.     (*textFilePtr) = fopen(wordPtr, "r");
  49.  
  50.     if ((*textFilePtr) == NULL) {
  51.         printf("It was not possible to open the file.\n");
  52.         return 0;
  53.     }
  54.  
  55.     fgetpos((*textFilePtr), &(*textFilePos));
  56.     return 1;
  57. }
  58. void fillList(FILE **textFilePtr, struct PlayerScore **currentPtr, struct PlayerScore **tailPtr, char test[20], char linePtr[20])
  59. {
  60.     int i = 0;
  61.     while ((fgets(linePtr, 256, *textFilePtr) != NULL))  {
  62.         i++;
  63.  
  64.         if (strstr(linePtr, test)) {
  65.             (*currentPtr)->name = *textFilePtr;
  66.             (*currentPtr) = (*currentPtr)->next;
  67.             printf("%d, ", i);
  68.            
  69.         }
  70.     }
  71.  
  72.  
  73. }
  74. int main()
  75. {
  76.     struct PlayerScore *root = NULL;
  77.     struct PlayerScore *current = NULL;
  78.     struct PlayerScore *tail = NULL;
  79.  
  80.     FILE *textFile = NULL;
  81.     fpos_t textFilePos;
  82.    
  83.     int running = 1;
  84.     int workingFile;
  85.     char line[20];
  86.     char test[20] = "?";
  87.     char file[20] = "highscore.txt";
  88.    
  89.     workingFile = openFile(&textFile, file, &textFilePos);
  90.     root = malloc(sizeof(struct PlayerScore));
  91.     root->next = NULL;
  92.     current = root;
  93.     fillList(&textFile, &current, &tail, test, line);
  94.     while (running)
  95.     {
  96.         Input(&current);
  97.         newStruct(&current);
  98.         Input(&current);
  99.         running = 0;
  100.     }
  101.     current = root;
  102.     OutPutt(&current, &tail);
  103.    
  104.     FreeMemory(&root);
  105.     if (textFile != NULL) {
  106.         fclose(textFile);
  107.     }
  108.     scanf("%d", &running);
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement