0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#define BUFFER_SIZE 128

const char *FILE_GAME_DATA_PATH = "./game.txt";

struct game_tag
{
    char gname[20];
    struct game_tag *next;
} * head;

//struct game_tag g;
typedef struct game_tag GAME;

//file functions
void read_file();


//util functions.

int menu();
void print_game(GAME *game);
void release(GAME *data);



//core

void display_game();
void quite();

//link
int count_elements(GAME *elements);
int count();


int main(void)
{
    int selected;


    
    read_file();

    while (1)
    {
        selected = menu();

        switch (selected)
        {
        case 1:
            display_game();
            break;

        default:
            printf("cannot find your option!");
            break;
        }
    }
}


void display_game()
{
    read_file();

    GAME *game = head;

    if(game == NULL)
    {
        printf("\nNo Game!\n");
        return;
    }
    print_game(game);
}

void print_game(GAME *game)
{

    int records_count = 0;
    printf("\n========== GAME ==========\n");
    while(game != NULL)
    {
        printf("\n");
        printf("Game Name: %s\n ", game->gname);
        game = game->next;
        records_count++;
    }
    printf("\nRecords: %d has been loaded successfully!\n", records_count);
    release(game);
}
int menu()
{
    printf("\n(1) Display Game details\n");


    int choosen;
    printf("\nEnter your option: ");
    scanf("%d", &choosen);
    return choosen;
}

void add_game(char game_name[20])
{
    GAME *temp, *iterator;
    temp = (struct game_tag *)malloc(sizeof(struct game_tag));

    GAME info;
   memcpy(info.gname, game_name, 20);


    //temp = head;
    iterator = head;

    if (head == NULL)
    {
        head = temp;
        head->next = NULL;
    }
    else
    {
        while (iterator->next != NULL)
        {
            iterator = iterator->next;
        }
        temp->next = NULL;
        iterator->next = temp;
    }
}


void read_file()
{
    if(head != NULL)
    {
        GAME *temp;
        while(head != NULL)
        {
            temp = head;
            head = head->next;
            free(temp);
        }
    }

        FILE *file;
        file = fopen(FILE_GAME_DATA_PATH, "r");

        if(file == NULL)
        {
            printf("Cannot read file: %s", FILE_GAME_DATA_PATH);
            exit(EXIT_FAILURE);
        }

        char game_name[20];

    
        int i;
        while(!feof(file))
        {
            char no[BUFFER_SIZE];

            fgets(game_name, sizeof(game_name), file);

            i=0;
            while(game_name[i] != '\0')
            {
                i++;
            }
            game_name[i] = '\0';
            add_game(game_name);
        }
        fclose(file);

}

void quite()
{
    printf("\nGoodbye!");
    exit(EXIT_SUCCESS);
}

void release(GAME *data)
{
    if (data == NULL)
    {
        return;
    }

    // free the nodes
    // because it can be use in memory
    // we need to clear it first
    // before we re-initailize the new data
    GAME *temp;
    while (data != NULL)
    {
        temp = data;
        data = data->next;
        free(temp);
    }
}

(1) At this point, I have created the main function (2) I'm trying to print the value from txt file which is char value that less than 20byte (3) My error is it read the file properly, but it returns the garbage value !Error I'm concern that my read_file function doesn't read txt proper or there an error in my condition statement

Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
  • 2
    Take this as an oppurtunity to learn how to use a *debugger* (a crucial an necesarry tool to know). With a debugger you can step through your code statement by statement while monitoring variables and their values. – Some programmer dude Aug 23 '20 at 06:07
  • Agreed with @Someprogrammerdude, have a look at [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – sebastian Aug 23 '20 at 06:29
  • You might want to take a look at your `add_game` – sebastian Aug 23 '20 at 06:47

0 Answers0