由于写入和读取的大小不同,我无法在文件上写入和读取堆栈(节点的链接列表)。
谈到写作,这是我的职责:
int my_stack_write(struct my_stack *stack, char *filename){
int count = 0;
struct my_stack_node *aux =malloc(sizeof(struct my_stack_node));
FILE *file = fopen(filename, "wb");
if(stack->first != NULL){
aux = stack->first;
count++;
while(aux->next != NULL){
printf("New node in s1: (%p)\n", aux->data);
fwrite(aux ,sizeof(struct my_stack_node), 1, file);
aux = aux->next;
count++;
}
printf("New node in s1: (%p)\n", aux->data);
fwrite(aux ,sizeof(struct my_stack_node), 1, file);
}
fclose(file);
return count;
}
这是我的读物:
struct my_stack *my_stack_read(char *filename){
struct my_stack *stackRead = my_stack_init(sizeof(struct my_stack_node));;
struct my_stack_node *stackNode = malloc(sizeof(struct my_stack_node));
FILE *file = fopen(filename, "rb");
if(!file){
puts("Impossible obrir el fitxer");
return NULL;
}else{
while(fread(stackNode, sizeof(struct my_stack_node), 1, file)){
printf("New node in fs1: (%p)\n", stackNode->data);
stackNode = (struct my_stack_node*) stackNode;
my_stack_push(stackRead, stackNode);
}
fclose(file);
return stackRead;
}
}
我正在使用的测试是这个:
if (my_stack_write(s1, "/tmp/my_stack.data") != len1) {
puts("Error in my_stack_write (s1)");
exit(1);
}
puts("\nReading the file...");
fs1 = my_stack_read("/tmp/my_stack.data");
if (!fs1) {
puts("Error in my_stack_read (fs1)");
exit(1);
}
if (my_stack_len(s1) != my_stack_len(fs1)) {
printf("Stacks s1 (initial stack 1) %d and fs1 %d (retrieved from file) don't have the same length\n",my_stack_len(s1) ,my_stack_len(fs1));
exit(1);
}
puts("s1 and the one retrieved from file (fs1) have the same length.");
puts("Comparing the data...");
// Test we can free the data and compare stacks s1 and fs1
while ((data1 = my_stack_pop(s1))) {
data2 = my_stack_pop(fs1);
printf("Node of s1: (%d, %s)\t", data1->val, data1->name);
printf("Node of fs1: (%d, %s)\n", data2->val, data2->name);
if (!data2 || data1->val != data2->val || my_strcmp(data1->name, data2->name)) {
printf("Data in s1 and fs1 are not the same.\n (data1->val: %d <> data2->val: %d) o (data1->name: %s <> data2->name: "
"%s)\n",
data1->val, data2->val, data1->name, data2->name);
exit(1);
}
size1 = sizeof(*data1);
size2 = sizeof(*data2);
free(data1);
free(data2);
}
printf("size of data from s1: %d\t", size1);
printf("size of data from fs1: %d\n", size2);
只需更改测试,并设法读取“更好”,输出为:
Writting the smaller stack (s1), it must truncate the file.
New node in s1: (0x55969ed2be70)
New node in s1: (0x55969ed2be00)
New node in s1: (0x55969ed2bd90)
New node in s1: (0x55969ed2bd20)
New node in s1: (0x55969ed2bcb0)
New node in s1: (0x55969ed2bc40)
New node in s1: (0x55969ed2bbd0)
New node in s1: (0x55969ed2bb60)
New node in s1: (0x55969ed2baf0)
New node in s1: (0x55969ed2ba80)
Reading the file...
New node in fs1: (0x55969ed2be70)
New node in fs1: (0x55969ed2be00)
New node in fs1: (0x55969ed2bd90)
New node in fs1: (0x55969ed2bd20)
New node in fs1: (0x55969ed2bcb0)
New node in fs1: (0x55969ed2bc40)
New node in fs1: (0x55969ed2bbd0)
New node in fs1: (0x55969ed2bb60)
New node in fs1: (0x55969ed2baf0)
New node in fs1: (0x55969ed2ba80)
s1 and the one retrieved from file (fs1) have the same length.
所以我认为每次读取和写入都会写入和读取完全相同的节点,但是当我比较节点时:
Comparing the data...
Node of s1: (-1630355856, �U) Node of fs1: (-1630350448, �U)
Data in s1 and fs1 are not the same.
(data1->val: -1630355856 <> data2->val: -1630350448) o (data1->name: �U <> data2->name: �U)
它“爆炸”。知道为什么吗?
任何帮助都会起作用。