Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct PlayerScore {
- char name[20];
- int score;
- struct PlayerScore *next;
- };
- void newStruct(struct PlayerScore **currentPtr)
- {
- (*currentPtr)->next = malloc(sizeof(struct PlayerScore));
- (*currentPtr) = (*currentPtr)->next;
- (*currentPtr)->next = NULL;
- }
- void FreeMemory(struct PlayerScore **currentPtr)
- {
- struct PlayerScore *temp = NULL;
- while ((*currentPtr) != NULL) {
- temp = (*currentPtr);
- (*currentPtr) = (*currentPtr)->next;
- free(temp);
- }
- }
- void Input(struct PlayerScore **current)
- {
- printf("INPUT NAME!: ");
- fgets((*current)->name, 20, stdin);
- (*current)->name[strlen((*current)->name) - 1] = '\0';
- (*current)->score = 100;
- }
- void OutPutt(struct PlayerScore **currentPtr, struct PlayerScore **tailPtr)
- {
- while ((*currentPtr) != NULL) {
- printf("\nName: %s\nScore: %d\n", (*currentPtr)->name, (*currentPtr)->score);
- if ((*currentPtr)->next == NULL){
- (*tailPtr) = (*currentPtr);
- }
- (*currentPtr) = (*currentPtr)->next;
- }
- }
- int openFile(FILE **textFilePtr, char wordPtr[13], fpos_t *textFilePos)
- {
- (*textFilePtr) = fopen(wordPtr, "r");
- if ((*textFilePtr) == NULL) {
- printf("It was not possible to open the file.\n");
- return 0;
- }
- fgetpos((*textFilePtr), &(*textFilePos));
- return 1;
- }
- void fillList(FILE **textFilePtr, struct PlayerScore **currentPtr, struct PlayerScore **tailPtr, char test[20], char linePtr[20])
- {
- int i = 0;
- while ((fgets(linePtr, 256, *textFilePtr) != NULL)) {
- i++;
- if (strstr(linePtr, test)) {
- (*currentPtr)->name = *textFilePtr;
- (*currentPtr) = (*currentPtr)->next;
- printf("%d, ", i);
- }
- }
- }
- int main()
- {
- struct PlayerScore *root = NULL;
- struct PlayerScore *current = NULL;
- struct PlayerScore *tail = NULL;
- FILE *textFile = NULL;
- fpos_t textFilePos;
- int running = 1;
- int workingFile;
- char line[20];
- char test[20] = "?";
- char file[20] = "highscore.txt";
- workingFile = openFile(&textFile, file, &textFilePos);
- root = malloc(sizeof(struct PlayerScore));
- root->next = NULL;
- current = root;
- fillList(&textFile, ¤t, &tail, test, line);
- while (running)
- {
- Input(¤t);
- newStruct(¤t);
- Input(¤t);
- running = 0;
- }
- current = root;
- OutPutt(¤t, &tail);
- FreeMemory(&root);
- if (textFile != NULL) {
- fclose(textFile);
- }
- scanf("%d", &running);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement