#struct #dynamic-programming #display #readfile #feof
#struct #динамическое программирование #отобразить #readfile #feof
Вопрос:
#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: %sn ", 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 detailsn");
int choosen;
printf("nEnter your option: ");
scanf("%d", amp;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] != '')
{
i ;
}
game_name[i] = '';
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) На этом этапе я создал основную функцию
(2) Я пытаюсь напечатать значение из текстового файла, которое является значением char размером менее 20 байт
(3) Моя ошибка в том, что файл прочитан правильно, но он возвращает значение мусора
!Ошибка Я обеспокоен тем, что моя функция read_file не читает текст должным образом или в моем выражении condition ошибка
Комментарии:
1. Воспользуйтесь этим как возможностью научиться пользоваться отладчиком (крайне важным инструментом, который необходимо знать). С помощью отладчика вы можете пошагово выполнять код оператор за оператором, отслеживая переменные и их значения.
2. Согласен с @Someprogrammerdude, взгляните на как отлаживать небольшие программы
3. Возможно, вы захотите взглянуть на свой
add_game