将二进制文件加载到未知的结构类型

Loading binary file to unknown struct type

我在读取二进制文件并将其转换为未知类型的结构时遇到问题...

我正在用 C 编写一个通用链表,它打算成为一个 header 文件,因此我可以在任何地方使用它。由于它将成为通用类型列表,因此 header 将不知道列表中的数据类型(我正在查看混合类型,因此是结构)。为了保存数据,我只传递了从 sizeof(struct) 中提取的数据地址及其长度。读取是相同的概念,使用 fread(container, sizeof(struct), 1, FILE),它由调用程序传递,再次使用 sizeof(struct) 提取 se 大小。但实际上,它不起作用...

#ifndef LINKEDLIST_H_INCLUDED
#define LINKEDLIST_H_INCLUDED
#include <string.h>

typedef struct tagNode{
    void            *data;
    struct tagNode  *next_Node;
} Node;

typedef struct tagLinkedList{
    Node *Head;
    int   Size;
} LinkedList;

int LinkedList_New(LinkedList *llist){
    llist->Head = NULL;
    llist->Size = 0;
    return 1;
}

int LinkedList_Insert(LinkedList *llist, int index, void *Data, size_t s_Data){
    int cur_index = 0;
    if(index > llist->Size || index < 0)
        index = 0;

    Node *newNode = malloc(sizeof(Node));
    newNode->data = malloc(s_Data);
    if(newNode       == NULL){return 0;}
    if(newNode->data == NULL){return 0;}
    newNode->data = Data;

    Node *currentNode = llist->Head;
    Node *lastNode    = llist->Head;

    if(index == 0){
        newNode->next_Node = llist->Head;
        llist->Head = newNode;
    }else{
        while(llist->Head->next_Node != NULL && cur_index != index){
            if(cur_index == index){
                newNode->next_Node  = currentNode;
                lastNode->next_Node = newNode;
            }else{
                lastNode    = currentNode;
                currentNode = currentNode->next_Node;
                cur_index++;
            }
        }
    }
    llist->Size += 1;
}

int LinkedList_Save(char *Path, LinkedList *llist, size_t sData){
    FILE *fp;
    fp = fopen(Path, "w");
    if(fp == NULL){return -1;}

    Node *currentNode;
    currentNode = llist->Head;

    while(currentNode != NULL){
        fwrite(currentNode->data, sData, 1, fp);
        currentNode = currentNode->next_Node;
    }
    fclose(fp);
    return 1;
}

int LinkedList_Load(char *Path, LinkedList *llist, size_t sData){
    FILE *fp;
    fp = fopen(Path, "r");
    if(fp == NULL){fclose(fp);return -1;}

    while(!feof(fp)){
        void *Data = malloc(sData);
        if(Data == NULL){fclose(fp);return -1;}
        fread(Data, sData, 1, fp);
        LinkedList_Insert(llist, 0, Data, sData);
    }
    fclose(fp);
    return 1;
}
#endif // LINKEDLIST_H_INCLUDED

我目前的测试对象:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../LinkedList.h"
typedef struct{
    int a;
    char b[5];
} tempo;

int main(){
    tempo teste = {5, "oito"};

    LinkedList lista;
    LinkedList_New(&lista);
    LinkedList_Insert(&lista, 0, &teste, sizeof(tempo));
    LinkedList_Save("data.txt", &lista, sizeof(tempo));
    printf("%s", ((tempo*)lista.Head->data)->b);
    LinkedList ls2;
    LinkedList_New(&ls2);
    LinkedList_Load("data.txt", &ls2, sizeof(tempo));

    printf("%s", ((tempo*)ls2.Head->data)->b);

    return 1;
}

第一个 printf 向我显示了结构中的 b 变量,这意味着该列表按预期工作。

但是第二个 printf,如果用于显示 a 变量(int),我得到一个随机数(类似于 8712382),如果用于显示 b 变量,我只得到 "L"

你的 LinkedList_load 功能有问题。将其更新为以下

int LinkedList_Load(char *Path, LinkedList *llist, size_t sData){
        FILE *fp;
        fp = fopen(Path, "rb");
        if(fp == NULL){fclose(fp);return -1;}
        fseek(fp,0L,SEEK_SET);
        while(!feof(fp)){
                void *Data = malloc(sData);
                if(Data == NULL){fclose(fp);return -1;}
                int readed=fread(Data, sData, 1, fp);
                if(readed==0){return -1;}
                /*you were displaying the last reading that contains 
                *nothing, the previous check solves the problem.
                */
                printf("readed %d items:  \n",readed);
                LinkedList_Insert(llist, 0, Data, sData);       
        }
       fclose(fp);

}