在C中通过二进制文件进行IO

在C中通过二进制文件进行IO,c,io,binaryfiles,C,Io,Binaryfiles,如何正确使用c脚本将自定义struct保存为二进制文件并从另一个c脚本读取 以下代码分为两部分保存结构部分创建自定义结构并将其保存为二进制文件读取结构部分尝试读取二进制文件 如果我在两个部分都在其中的情况下运行整个脚本,那么read部分可以很好地获得结构。但是,如果我先注释掉读取部分并运行保存部分,然后注释掉保存部分,然后运行读取部分。它坏了,出现了一个分割错误 #include <stdio.h> #include <stdlib.h> struct Case_dic

如何正确使用c脚本将自定义
struct
保存为二进制文件并从另一个c脚本读取

以下代码分为两部分<代码>保存结构部分创建自定义结构并将其保存为二进制文件<代码>读取结构部分尝试读取二进制文件

如果我在两个部分都在其中的情况下运行整个脚本,那么read部分可以很好地获得结构。但是,如果我先注释掉
读取部分
并运行
保存部分
,然后注释掉
保存部分
,然后运行
读取部分
。它坏了,出现了一个分割错误

#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;
}
#包括
#包括
结构实例{
整数整圈大小;
int num_集群;
int*集群大小;
结构集群*所有集群问题;
};
结构簇{
int cluster_idx,num_实例,num_量子位;
结构实例*实例;
};
结构实例{
int cluster_idx,instance_idx;
int*init,*meas;
};
int main(int argc,char*argv[])
{
//保存结构节
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));
实例->集群_idx=3;
实例->实例_idx=3;
实例->初始化=初始化;
实例->meas=meas;
struct Cluster*Cluster=malloc(sizeof(struct Cluster));
集群->集群_idx=3;
集群->num_实例=5;
集群->数量量子比特=10;
集群->实例=实例;
printf(“已完成构建结构群集\n”);
文件*output_fptr=fopen(“test.bin”、“wb”);
fwrite(cluster,sizeof(struct cluster),1,output_fptr);
fclose(输出_fptr);
printf(“已完成二进制写入\n”);
//读取结构部分
结构簇簇2;
文件*input_fptr=fopen(“test.bin”、“rb”);
fread(&cluster_2,sizeof(struct cluster),1,input_fptr);
fclose(输入_fptr);
printf(“cluster_idx=%d,%d个实例,%d个量子位\n”,cluster_2.cluster_idx,cluster_2.num_实例,cluster_2.num_量子位);
struct Instance*instances_2=cluster_2.instances;
int i;
printf(“群集%d的实例%d\n”,实例_2[0]。实例_idx,实例_2[0]。群集_idx);
printf(“init_meas:”);

对于(i=0;i
struct Cluster
包含一个指针。您正在将该地址写入文件。当您的进程从文件加载数据时,存储在文件中的地址无效。您所拥有的只是那些无效的指针成员。您认为它指向的数据在哪里?因此答案是您不能。您要么没有o找到一种方法逐段存储实际数据,或将所有数据包含在
结构中。
一种方法是将数据保存为文本文件,并将其读回结构成员中(就像从键盘一样)。这样做()帮助?感谢您的解释。我现在了解到,要存储指针,我必须编写自己的序列化/反序列化代码。
struct Cluster
包含指针。您正在将该地址写入文件。当您的进程从文件加载数据时,文件中存储的地址无效。您所拥有的这些是无效的指针成员。您认为它指向的数据在哪里?因此,答案是您不能。您必须找到一种方法逐段存储实际数据,或者将所有数据包含在
结构中。
一种方法是将数据保存为文本文件,并将其读回结构成员中(好像是从键盘上)。这个()有用吗?谢谢你的解释。我现在明白了,要存储指针,我必须编写自己的序列化/反序列化代码。