c - 将链接列表的链接列表保存到二进制文件

标签 c

我正在尝试制作一个仅包含第一个数字(例如“ 123456789”)和以下文本文件中名称的二进制文件:

123456789 0 Paulo Silva

555666777 2 Marta Nunes Cabral
1   1   12  12  2017    17  12  2017
2   0   20  04  2018

123123123 3 Maria Pimentel
2   2   01  10  2017    10  10  2017
2   1   13  01  2018    15  01  2018
1   0   17  04  2018


这是我到目前为止的代码:

void gravaClientesBanidos(pClientes cli){
    FILE *f;
    int total = 0;

    f = fopen("Banidos.dat", "wb");
    if(f == NULL)
        printf("Erro ao escrever o ficheiro");

    while(cli != NULL){
        if(cli->banido == true){
            fwrite(cli, sizeof(clientes), 1, f);
            cli = cli->prox;
            total++;
        }
    }

    fwrite(&total, sizeof(int), 1, f);
    fclose(f);
}


前提是,如果禁止客户端,则应将其自动放入该二进制文件中。我相信,当我将其保存到二进制文件时,它会保存第一行,但是当我尝试读取文件时,它只会使程序崩溃

这是读取的代码:

void leClientesBanidos(){
    FILE *f;
    pClientes novo;
    int i, total;

    f = fopen("Banidos.dat", "rb");
    if(f == NULL)
        printf("Não existe dados para ler!");


    fseek(f, sizeof(int), SEEK_END);
    fread(&total, sizeof(int), 1, f);

    for(i = total - 1; i >= 0; i--){
        fseek(f, sizeof(clientes) * i, SEEK_SET);
        fread(novo, sizeof(clientes), 1, f);
    }
    printf("NIF: %d\nNome: %s", novo->NIF, novo->nome);
    fclose(f);
}


编辑:忘了把这种情况下的结构

typedef struct Alugueres alugueres, *pAlugueres;
typedef struct Clientes clientes, *pClientes;

struct Alugueres{
    int ID;
    int estado; //0 - A decorrer, 1 - Entregue, 2 - Entregue Danificada
    data dataInicio;
    data dataFim;
    data dataEntrega;
    pAlugueres prox;
};

struct Clientes{
    char nome[25];
    int nAlugueres;
    int NIF;
    int razaoBanido; //0 - atraso, 1 - Guitarras danificadas, 2 - Nao ha razao
    bool banido; //0 - Nao, 1 - SIM
    int count; //Contagem das guitarras danificadas entregues
    pAlugueres al;
    pClientes prox;

};


更新:因此,我认为我发现了问题,当我尝试读取“总计”整数(即存储在文件末尾的文件中的结构数)时,它只是用垃圾填充而不是正确的数字。 ..但是,如果我手动将total = 3放在读取函数中,则可以正常工作。

显然,这不是我想要的正确方法,有人对如何解决此问题有意见吗?

最佳答案

pClientes novo声明未初始化的指针。您需要clientes novo
要读取文件末尾附近的内容,请使用:

int offset = sizeof(int);
fseek(f, -offset, SEEK_END);

请尝试以下方法。
void read()
{
    FILE *f;
    int i, total;
    clientes novo;

    f = fopen("Banidos.dat", "rb");
    if(f == NULL)
        printf("Não existe dados para ler!");

    int offset = sizeof(int);
    fseek(f, -offset, SEEK_END);
    fread(&total, sizeof(int), 1, f);

    fseek(f, 0, SEEK_SET);
    for(i = 0; i < total; i++)
    {
        fread(&novo, sizeof(clientes), 1, f);
        printf("NIF: %d, Nome: %s\n", novo.NIF, novo.nome);
    }

    fclose(f);
}

关于c - 将链接列表的链接列表保存到二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50741049/

相关文章:

创建指向内存地址的符号链接(symbolic link)。 (剧透 : Stripe CTF contest)

c - 在 C 中获取 Fat12 磁盘的卷标

c - 如何在C中显示链表中节点的地址

arrays - 在cuda GPU cudamalloc中将3D数组转换为1D

c - 错误: conflicting types for 'exit' in OS header 'proc.h'

java - SendMessage() 正在发送多条消息,而不是只发送一条消息

c - 如何在不显式链接数学库的情况下使用带有 gcc 的 c 中的数学函数

c - 如何将虚拟磁盘的内存设置为 0

c - 共享库中未定义的外部符号

c - 获取 linux 可执行文件加载地址(__builtin_return_address 和 addr2line)