1

How do I properly use a c script to save a custom struct as a binary file and read it from another c script?

There are two sections to the codes below. Save struct section creates and saves a custom struct as a binary file. Read struct section attempts to read the binary file.

If I run the entire script with both sections in there, the read section gets the struct just fine. However, if I comment out the read section and just run the save section first, and comment out the save section and just run the read section afterwards. It broke and gave a segmentation error.

#include <stdio.h>
#include <stdlib.h>

struct Case_dict {
    int full_circ_size;
    int num_clusters;
    int *cluster_circ_sizes;
    struct Cluster *all_cluster_prob;
};

struct Cluster {
    int cluster_idx, num_instance, num_qubits;
    struct Instance *instances;
};

struct Instance {
    int cluster_idx, instance_idx;
    int *init, *meas;
};

int main(int argc, char *argv[])
{
    // Save struct section
    int *init = malloc(4*sizeof(int));
    init[0] = 0;
    init[1] = 1;
    init[2] = 2;
    init[3] = 3;
    int *meas = malloc(4*sizeof(int));
    meas[0] = 4;
    meas[1] = 5;
    meas[2] = 6;
    meas[3] = 7;
    struct Instance *instance = malloc(sizeof(struct Instance));
    instance->cluster_idx = 3;
    instance->instance_idx = 3;
    instance->init = init;
    instance->meas = meas;

    struct Cluster *cluster = malloc(sizeof(struct Cluster));
    cluster->cluster_idx = 3;
    cluster->num_instance = 5;
    cluster->num_qubits = 10;
    cluster->instances = instance;

    printf("Finished building struct Cluster\n");

    FILE *output_fptr = fopen("test.bin","wb");
    fwrite(cluster, sizeof(struct Cluster), 1, output_fptr);
    fclose(output_fptr);

    printf("Finished writing binary\n");

    // Read struct section
    struct Cluster cluster_2;
    FILE *input_fptr = fopen("test.bin","rb");
    fread(&cluster_2, sizeof(struct Cluster), 1, input_fptr);
    fclose(input_fptr);

    printf("cluster_idx = %d, %d instances, %d qubits\n",cluster_2.cluster_idx,cluster_2.num_instance,cluster_2.num_qubits);
    struct Instance *instances_2 = cluster_2.instances;
    int i;
    printf("Instance %d of cluster %d\n",instances_2[0].instance_idx,instances_2[0].cluster_idx);
    printf("init_meas: ");
    for (i=0;i<4;i++) {
        printf("%d ",instances_2[0].init[i]);
    }
    for (i=0;i<4;i++) {
        printf("%d ",instances_2[0].meas[i]);
    }
    printf("\n");
    return 0;
}
FSeed
  • 31
  • 4
  • 5
    `struct Cluster` contains a pointer. You are writing that address into the file. When your process loads the data from the file, the address that as stored in the file is not valid. – William Pursell Mar 21 '20 at 20:26
  • 1
    And all you have is those invalid pointer members. Where do you suppose the data it pointed to is? So the answer is that you can't. You either have to find a way to store the actual data piece by piece, or include all the data within the `struct.` One way to do it, is to save the data as a text file, and read it back into the struct members as such (as if from the keyboard). – Weather Vane Mar 21 '20 at 20:38
  • does this (https://stackoverflow.com/questions/2763438/write-pointer-to-file-in-c) help? – hanie Mar 21 '20 at 20:45
  • 1
    Thanks for the explanation. I now understand that to store pointers I will have to write my own serialization/deserialization codes. – FSeed Mar 22 '20 at 01:59

0 Answers0