C - 使用 fwrite 在其他结构数组中创建一个结构数组的 bin 文件

C - create a bin file of a structs's array inside other struct's array with fwrite

我在其他结构的数组中有一个结构的数组,我想创建一个包含此数据的二进制文件(只有元素不为空)。

我的结构是:

struct viaje {
    char identificador[30+1];
    char ciudadDestino[30+1];
    char hotel[30+1];
    int numeroNoches;
    char tipoTransporte[30+1];
    float precioAlojamiento;
    float precioDesplazamiento;
};

struct cliente {
    char dni[30+1];
    char nombre[30+1];
    char apellidos[30+1];
    char direccion[30+1];
    struct viaje viajes[50];
    int totalViajes;
} clientes[20];

接下来我要尝试:

// For create bin file
for (i = 0; i < totalClientes; i++) {
    fwrite(clientes[i], sizeof(struct cliente), 1, fp_guardarCargarEstado);
    for (j = 0; j < clientes[i].totalViajes; j++) {
        fwrite(clientes[i].viajes[j], sizeof(struct viaje), 1, fp_guardarCargarEstado);
    }
}


// For read bin file
for (i = 0; i < totalClientes; i++) {
    fread(clientes[i], sizeof(struct cliente), 1, fp_guardarCargarEstado);
    for (j = 0; j < clientes[i].totalViajes; j++) {
        fread(clientes[i].viajes[j], sizeof(struct viaje), 1, fp_guardarCargarEstado);
    }
}

我还没有尝试 fread 因为我在 fwrite 中遇到两个错误:error: incompatible type for argument 1 of 'fwrite'note: expected 'const void *' but argument is of type 'struct cliente'

为什么会这样?

freadfwrite 函数将指向内存位置的指针作为它们的第一个参数。您传递的是一个结构实例。

您需要使用address-of 运算符& 传入此结构的地址。此外,不需要单独编写 struct viaje 个实例,因为它们已经包含在 struct cliente

// For create bin file
for (i = 0; i < totalClientes; i++) {
    fwrite(&clientes[i], sizeof(struct cliente), 1, fp_guardarCargarEstado);
}


// For read bin file
for (i = 0; i < totalClientes; i++) {
    fread(&clientes[i], sizeof(struct cliente), 1, fp_guardarCargarEstado);
}

这里似乎发生了一些事情。这里有一些重要的事情需要注意。

  1. totalViajesstruct cliente 中的位置
  2. 你想在fwrite()中写入的字节数
  3. 在再次读取文件之前重置 FILE*

这是我用来测试我认为你正在寻找的东西。

struct viaje {
    char identificador[30+1];
    char ciudadDestino[30+1];
    char hotel[30+1];
    int numeroNoches;
    char tipoTransporte[30+1];
    float precioAlojamiento;
    float precioDesplazamiento;
};
struct cliente {
    int totalViajes;
    char dni[30+1];
    char nombre[30+1];
    char apellidos[30+1];
    char direccion[30+1];
    struct viaje viajes[50];
} clientes[20];

int main()
{
    clientes[0].totalViajes = 1;
    clientes[0].viajes[0].numeroNoches = 52;
    int totalClientes = 1;

    FILE* fp_guardarCargarEstado = fopen("myFile.bin", "wb");
    // For create bin file
    for (int i = 0; i < totalClientes; i++) {
        fwrite(&clientes[i], sizeof(struct cliente)-(sizeof(struct viaje)*50), 1, fp_guardarCargarEstado);
        for (int j = 0; j < clientes[i].totalViajes; j++) {
            fwrite(&clientes[i].viajes[j], sizeof(struct viaje), 1, fp_guardarCargarEstado);
        }
    }
    fclose(fp_guardarCargarEstado);

    // set variables to 0 so you can tell if the read actually does anything
    clientes[0].totalViajes = 0;
    clientes[0].viajes[0].numeroNoches = 0;

    fp_guardarCargarEstado = fopen( "myFile.bin", "rb" );
    // For read bin file
    for (int i = 0; i < totalClientes; i++) {
        fread(&clientes[i], sizeof(struct cliente)-(sizeof(struct viaje)*50), 1, fp_guardarCargarEstado);
        for (int j = 0; j < clientes[i].totalViajes; j++) {
            fread(&clientes[i].viajes[j], sizeof(struct viaje), 1, fp_guardarCargarEstado);
        }
    }

    fclose(fp_guardarCargarEstado);

    printf("%i\n%i", clientes[0].totalViajes, clientes[0].viajes[0].numeroNoches );

    return 0;
}