因此我使用二进制文件来保存有关某些节点(系统内部的某些节点)状态的信息。关键是这个二进制文件只是很多1和0,并且想法是读取文件并将其加载到结构中。 这是结构的定义:
typedef struct t_bitmap{
int estado;
struct t_bitmap* siguiente;
}t_bitmap;
这是应该加载它的代码:
t_bitmap leerBitmap(char* unPath){
t_bitmap bitmap;
FILE *fp = fopen (unPath, "rb");
int i=0;
fseek(fp, 0, SEEK_END);
int tamanio = sizeof(char) * ftell(fp);
fseek(fp, 0, SEEK_SET);
char* bytes = malloc(tamanio);
fread(bytes, tamanio, 1, fp);
fclose (fp);
while(i<tamanio){
bitmap.estado = bytes[i];
bitmap = bitmap.siguiente; //This fails
i++;
};
free(bytes);
return bitmap;
};
编辑1
错误是: 从类型'struct t_bitmap *'
分配类型't_bitmap'时不兼容的类型答案 0 :(得分:1)
您需要为读入的每个字节分配一个新节点。
通常会定义函数,使其返回指向链表头部的指针(如果没有值可以读入,则可以是NULL
。)
为了不改变你的函数的原型,我保留了列表头部的“按值返回” - 隐喻。
因此函数为每个字节分配一个新节点,第一个字节除外,它直接存储在将由值返回的“head”中:
t_bitmap leerBitmap(char* unPath){
t_bitmap bitmap;
FILE *fp = fopen (unPath, "rb");
int i=0;
fseek(fp, 0, SEEK_END);
int tamanio = sizeof(char) * ftell(fp);
fseek(fp, 0, SEEK_SET);
char* bytes = malloc(tamanio);
fread(bytes, tamanio, 1, fp);
fclose (fp);
t_bitmap* curBitMap = &bitmap; // the current bitmap to write to
while(i<tamanio){
if (i > 0) { // except for the first, create a new node
curBitMap->siguiente = malloc(sizeof(t_bitmap));
curBitMap = curBitMap->siguiente;
}
curBitMap->estado = bytes[i];
curBitMap->siguiente = NULL;
i++;
};
free(bytes);
return bitmap;
}