我在从二进制文件读取并将其转换为未知类型的结构时遇到问题...
我正在用C语言编写一个通用链表,它打算用作头文件,因此我可以在任何地方使用它。由于它将是通用类型列表,因此标头将不知道列表中数据的类型(我正在查看混合类型,因此是结构)。为了保存数据,我只传递了从sizeof(struct)中提取的数据地址及其长度。读取是相同的概念,使用fread(container,sizeof(struct),1,FILE),该值由调用程序传递,再次使用sizeof(struct)提取大小。但是实际上,它不起作用...
#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”
答案 0 :(得分:1)
您的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);
}