C - 使用fwrite

时间:2018-03-07 17:04:49

标签: c struct fwrite

我在其他struct的数组中有一个struct的数组,我想创建一个包含这些数据的二进制文件(只有元素不为null)。

我的结构是:

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中遇到两个错误:fwriteerror: incompatible type for argument 1 of 'fwrite'

,我还没有尝试note: expected 'const void *' but argument is of type 'struct cliente'

为什么会这样?

2 个答案:

答案 0 :(得分:3)

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 :(得分:1)

看起来这里发生了一些事情。这里有一些重要的事情需要注意。

  1. totalViajes
  2. struct cliente的位置
  3. 要在fwrite()
  4. 中写入的字节数
  5. 在重新读取文件之前重置FILE*
  6. 以下是我用来测试我认为你正在寻找的内容。

    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;
    }