fwrite does not write to file C

2

I have a problem with a program that I am writing.

What I intend is to open a file written in binary with account data, this includes a header with information of the data it contains. What I'm doing is reading that file and the data writing it into another file, as well as a backup.

I show a small part of the code:

typedef struct _Data{
 long id;
 char nombre[50];
 char email[50];
 char cuenta[10];
 float saldo;
 char estatus;;
}Data; 

typedef struct _Header{ long filesize; long datasize; short offsetData; short headerSize; short regSize; char type[4]; }Header;

void* codigoHilo(void *param){

altas = fopen("Files/alta.bd","rb"); base = fopen("Files/Banco.bd","wb+"); rewind(altas); fread(&header,1,sizeof(header),altas); for(i=1;i<=(header.datasize/header.regSize);i++){ fread(&elementos,1,sizeof(Data),altas); fwrite(&elementos,1,sizeof(Data),base); printf("Dada de alta cuenta: %s \n",elementos.cuenta); }

When executing it, it reads the file without problem, but does not write anything in the bank file. Once the execution is finished, a 0 bytes file is created.

Someone can help me get back on track.

Thank you.

    
asked by Salvador Mendez 11.06.2017 в 02:14
source

1 answer

0

Leaving aside the fact that your program does not compile. Your program works well if there is input data.

I created input data with the following program:

#include <stdio.h>
void creaDatos()
{
    FILE* altas; 
    altas = fopen("Files/alta.bd","wb");
    Header header;
    Data datos;
    header.regSize = sizeof(Data);
    header.datasize = sizeof(Data)*50;
    fwrite( &header, sizeof(header), 1, altas);
    Data data;
    for (int n=0; n<50; ++n)
    {
        data.id = n;
        sprintf(data.nombre, "Jose%d", n);
        sprintf(data.email, "[email protected]%d", n);
        sprintf(data.cuenta, "CC%d", n);
        data.saldo = 1000*n;
        data.estatus = 'A';
        fwrite( &data, sizeof(data), 1, altas);
    }
    fclose(altas);
}
int main(int argc, char** argv) {
    creaDatos();
}

To the previous program we must add the appropriate data types, that is, the same structures defined in the question, I do not repeat it here so as not to lengthen it unnecessarily.

Having the file Files / alta.bd created with the previous program step to execute the program of the question with slight corrections so that it compiles:

void* codigoHilo(void *param){
    FILE* altas;
    FILE* base;
    Data elementos;

    altas = fopen("Files/alta.bd","rb");

    base = fopen("Files/Banco.bd","wb+");

    rewind(altas);

    Header header;

    fread(&header,1,sizeof(header),altas);

    for(int i=1;i<=(header.datasize/header.regSize);i++){

        fread(&elementos,1,sizeof(Data),altas);

        fwrite(&elementos,1,sizeof(Data),base);

        printf("Dada de alta cuenta: %s \n",elementos.cuenta);
    }
}

And it works fine. Create a file Files / Bank.bd that is a copy of the previous one except for the header.

    
answered by 11.06.2017 / 07:54
source