Запись и чтение стека связанных списков в файле не совпадают - PullRequest
0 голосов
/ 03 января 2019

У меня проблемы с записью и чтением стека (связанного списка узлов) в файле, так как запись и чтение имеют разные размеры.

Когда речь идет о записи, этомоя функция:

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)

оно "взрывается".Любая идея, почему это?

Любая помощь будет работать.

...