功能误解

Function misunderstanding

#include <stdio.h>
typedef struct Forca // definining struct here
{
    char palavra[TAM_PALAVRA];
    char palavra_mascarada[TAM_PALAVRA];
    int erros, acertos, tentativas;

} t_forca;
void salva_jogo(t_forca forca) //function that writes structure inside bin file
{
    FILE* save;
    save = fopen("save.bin", "w+b");
    if (save == NULL)
    {
        printf("\nerro no arquivo\n");
    }
    fwrite(&forca, sizeof(forca), 1, save);
    fclose(save);
}
void carrega_jogo(t_forca* forca) //function that read struct inside bin file
{
    FILE* readsave;
    readsave = fopen("save.bin", "r+b");
    if (readsave == NULL)
    {
        printf("\nerro no arquivo\n");
    } //printf error
    fread(forca, sizeof(forca), 1, readsave);
    fclose(readsave);
}

基本上我正在尝试保存和读取二进制文件中的结构,但我很迷茫,因为文件正在写入但根本没有读取

在函数carrega_jogo中,forca是一个指针,sizeof(forca)与指针的大小相同,是4或8字节,取决于你的系统或编译器设置.读取函数最终只读取了 4 或 8 个字节。结构的其余部分可能未初始化并导致未定义的行为。

正确的版本应该是sizeof(t_forca)

另外,fwrite/fread "wb""rb".

就足够了