如何从文件 c 程序中打印 char 值?

How do i print the char value from file c program?

提问人:Phuk Munheng 提问时间:8/23/2020 最后编辑:Daniel WalkerPhuk Munheng 更新时间:8/24/2020 访问量:49

问:

#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)至此,我已经创建了 main 函数 (2)我正在尝试从txt文件中打印值,该值是小于20字节的字符值 (3)我的错误是它正确读取文件,但它返回垃圾值 !错误:我担心我的 read_file 函数无法正确读取 txt 或我的条件语句中有错误

struct 动态编程 显示 readfile feof

评论

2赞 Some programmer dude 8/23/2020
以此为契机学习如何使用调试器(一个关键的、需要了解的工具)。使用调试器,可以在监视变量及其值时逐个语句单步执行代码。
0赞 sebastian 8/23/2020
同意@Someprogrammerdude,看一看如何调试小程序
0赞 sebastian 8/23/2020
你可能想看看你的add_game

答: 暂无答案